How to use LocalFileSystem.getInstance().refreshAndFindFileByNIO() method in a thread where only read allowed?
Answered
I'm trying to find a file in SDK after the SDK is downloaded. However, I cant get file using the LocalFileSystem.getInstance().FindFileByNioFile(path) method cause the LocalFileSystem is not latest. Next ,I try to use refreshAndFindFileByNioFile(path) method in a writeAction,
ApplicationManager.getApplication()
.runWriteAction((Computable<VirtualFile>) () -> lfs.refreshAndFindFileByNioFile(path));
but end up get error message,
java.lang.Throwable: Write-unsafe context! Model changes are allowed from write-safe contexts only. Please ensure you're using invokeLater/invokeAndWait with a correct modality state (not "any"). See TransactionGuard documentation for details.
current modality=ModalityState.NON_MODAL
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:182)
at com.intellij.openapi.application.TransactionGuardImpl.assertWriteActionAllowed(TransactionGuardImpl.java:140)
at com.intellij.openapi.vfs.newvfs.RefreshQueueImpl.queueSessionSync(RefreshQueueImpl.java:68)
at com.intellij.openapi.vfs.newvfs.RefreshQueueImpl.execute(RefreshQueueImpl.java:54)
at com.intellij.openapi.vfs.newvfs.RefreshSessionImpl.launch(RefreshSessionImpl.java:115)
at com.intellij.openapi.vfs.newvfs.RefreshQueueImpl.processSingleEvent(RefreshQueueImpl.java:200)
at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.createAndFindChildWithEventFire(VirtualDirectoryImpl.java:309)
at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.findChild(VirtualDirectoryImpl.java:82)
at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.refreshAndFindChild(VirtualDirectoryImpl.java:346)
at com.intellij.openapi.vfs.newvfs.VfsImplUtil.refreshAndFindFileByPath(VfsImplUtil.java:121)
at com.intellij.openapi.vfs.impl.local.LocalFileSystemBase.refreshAndFindFileByPath(LocalFileSystemBase.java:69)
at com.intellij.openapi.vfs.LocalFileSystem.refreshAndFindFileByNioFile(LocalFileSystem.java:51)
So ,how can I refresh LocalFileSystem instance in a thread where only read allowed?
Thank you!
Please sign in to leave a comment.
Please show full stacktrace, not arbitrarily cut-off one.
@Yann Cebron ,Thank you for answering this, the whole error message is that
MyJsonSchemaFileProvider is my custom schemaFileProvider implemention.
Why do you need to lookup that file in filesystem? Why on every single call?
@Yann Cebron,In my situation, the schema file of config file changes under certain conditions. So I just look up fileSysytem when searching for schema file in method getSchemaFile(), But when this method is called, the thread state always only allow read , So I cant refresh fileSystem to get the latest schema file.
Where is the schema file actually located physically?
@Yann Cebron, the schema file is under my user directory C:\Users\yaphets123\AppData\Local\myshema.json. This file may not esixts, but when I copy one to this directory, LocalFileSystem cant sense the existence of the schema file, So I'm trying to refresh the localFileSystem, but got error msg presented above.
Please try using com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory#getResourceFile it should accept absolute URL (see com.intellij.openapi.vfs.VfsUtil#findFileByURL)