1

We have three separate apps which are in same App Group and access the same CoreData store. Problem is that when I change something in item in NSOrderedSet from relationship in managed object, save go to another app where refresh is performed, changed data are not there.

We are using NSPersistentContainer and only one context in each app, container.newBackgroundContext() saved to property in singleton. For each app when app goes to BG save() is performed on that context and when app goes to FG refreshAllObjects() is called on the context.

When I change some basic attribute in managed object it is changed properly in another app. But when I change some property in item from NSSet which is a relationship on managed object this change is not visible in another app.

While I was debugging I tried to call fetch but it also provides only old data. Only when I called context.reset() and then fetch again it returns valid new data.

Problem is that I cannot use reset on whole context because I will lose all registered objects in app.

Is this valid behavior or bug that referenced objects changes are not applied when refreshAllObjects() is used?

Is there any way how to force fetch request to get data directly from the database and not cached one from context?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
LukasP
  • 21
  • 1
  • 5
  • Have you tried [refresh(...)](https://developer.apple.com/documentation/coredata/nsmanagedobjectcontext/1506224-refresh) rather than refreshAllObjects()? – Joakim Danielson Mar 26 '18 at 09:30
  • @JoakimDanielson Yes I tried use just `refresh` on whole object but it did not work as well. It also check boolean values of `hasChanges` on context as well as on managed object. But these are only indicating local unsaved changes as mentioned in documentation. – LukasP Mar 26 '18 at 18:56

1 Answers1

1

I found one solution to my problem. I was able to force fetch to fetch directly from persistence (not from context) by setting shouldRefreshRefetchedObjects property of NSFetchRequest to true.

I was not able to find a solution to just refresh already fetched objects - but this way I was able to get fresh data using new fetch at least.

LukasP
  • 21
  • 1
  • 5
  • 1
    I have a similar setup as you and I added this property to my request in my NSFetchedResultsController and it didn't refresh the objects. Do you have any example code? – fphelp May 20 '20 at 09:43