How to force refresh config file(xml file) of PersistentStateComponent

Answered
@State(name = "xxx", storages = {@Storage("xxx.xml")})
public class XxxComponent implements PersistentStateComponent<CollectionConfiguration> {

}

when some operation change the XxxComponent, the config file xxx.xml will not change immediately, but when on frame deactivation it will be refreshed,

But i want to get the newest xml content of xxx.xml when XxxComponent changed. 

I have try the follow code, but it do not work

VirtualFile xmlVirtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
xmlVirtualFile.refresh(false, false);

 

So is there any way to force refresh xxx.xml or trigger the refresh  operation of PersistentStateComponent

 

 

2 comments
Comment actions Permalink

Hi Wilson,

There is no open API to force saving PersistentStateComponent. Why do you need that?

0
Comment actions Permalink

Thanks for your reply Karol!

XML file persistence will sometimes delay after PersistentStateComponent has changed.

Finally i have find the solution, Use scheduleProjectSave can force saving PersistentStateComponent xml file,

and then use Timer to get the last content from xml config file

 

SaveAndSyncHandler.getInstance().scheduleProjectSave(myProject);


new java.util.Timer().schedule(new TimerTask() {
    @Override
    public void run() {
        ApplicationManager.getApplication().invokeLater(() -> {
            if (i == 0) {
                FileUtil.writeString(content, path, StandardCharsets.UTF_8);//override config xml 
                xmlVirtualFile.refresh(false, false);//refresh
            }
        });
    }
},3000);

new java.util.Timer().schedule(new TimerTask() {
    @Override
    public void run() {
        ApplicationManager.getApplication().invokeLater(() -> {
            if (i == 0) {
                refresh();//UI refresh
                actionProgressBar.setVisible(false);
            }
        });
    }
},5000);
0

Please sign in to leave a comment.