1

i have a problem. My iOS app is behaving really strange when it comes to fetching some data and having unsaved changes. For your interest the whole behavior appears while syncing some data with a web server. I wanted to do a full sync and then save the changes. I tried some workarounds but none of them was working well enough.

To the problem itself: I sync some entities with a web server. They are organized into zones (their parent), which themselves are in a building. So for each entity i query if a matching zone already exists, and if not i create a new one. The problem now is that i'm unable to fetch those zones if they were just created (so a new but identical zone is created everytime). I also have the problem that i cannot fetch the correct building anymore once it is changed by adding a newly created zone to it, the result for the exact same query is suddenly empty.

I have ensured that [fetch setIncludePendingChanges:YES] is set, and i'm also using normal result mode not NSDictionaryResultType (see: NSDictionaryResultType expression not taking into account newly inserted objects).

I hope somebody can help.

Community
  • 1
  • 1
patman
  • 2,780
  • 4
  • 30
  • 54
  • Can you give any more details about how your NSManagedObjectContexts are configured? It sounds like you might be fetching from one context, while importing into another on a background thread. In which case you'll need to save the context (and merge the changes) to be able to fetch what's just been inserted. – Daniel Thorpe Jul 30 '12 at 09:35
  • Well i can try. I don't think i have multiple contexts - allways using the one from my AppDelegate. The synchronisation moreover is not done in a background thread, rather on the main thread while showing an activity indicator. – patman Jul 31 '12 at 07:11
  • Yeah, so update your question with your NSFetchedResultsController setup code. Is the activity indicator responsive, i.e. updating and animating smoothly, during the sync process? – Daniel Thorpe Jul 31 '12 at 09:00

2 Answers2

1

A Fetch request fetches data from a context that has saved data in the persistent store from which the context is fetching. When you create a new Managed Object, you create it in your context (a.k.a. your scratch book) but not in your persistent store, yet. So before you can fetch a newly created object, you must save the changes of that context into your store.

Eren Beşel
  • 1,047
  • 8
  • 16
  • 3
    According to the manual `[fetch setIncludePendingChanges:YES]` should do the trick [link](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html) – patman Jul 31 '12 at 07:06
  • Then it's likely about Objective-C type predicates. This [discussion](http://stackoverflow.com/a/7128251/692507) may be helpful – Eren Beşel Jul 31 '12 at 08:56
0

Assuming that I understand your description right: I think your predicate for fetching your data is pretty complex, which forces core data to read from the persistent store. Thus, modifications in the managed object context are ignored.

For example we have a data model like

Category 1---n Icon

and we want to fetch all categories which

  1. have icons (more than zero),
  2. have icons whose attribute usable is TRUE
  3. have icons whose attribute enabledByAdmin is TRUE

we use a predicate like this:

NSArray *predicates = @[[NSPredicate predicateWithFormat:@"icons.@count > 0"],
                        [NSPredicate predicateWithFormat:@"ANY icons.usable = 1"],
                        [NSPredicate predicateWithFormat:@"ANY icons.enabledByAdmin = 1"]];

NSCompoundPredicate *cp;
cp = [[NSCompoundPredicate alloc] initWithType:NSAndPredicateType
                                 subpredicates:predicates];

This complex predicate forces core data to read from the persistent store, directly.

My solution is to save the managed object context and fetch the data afterwards.

Hans One
  • 3,329
  • 27
  • 28
  • Hello! Thanks for your comment, but this problem occured a real long time ago and a lot of things changed since then. I'm pretty sure the predicate was very simple like `idfield = x` , where idfield was a custom one not the coredata id. I ended up caching it myself, saving the context was no option due to speed concerns. – patman Apr 13 '15 at 13:41
  • Hey patman, sad but true ... the problem with complex predicates still exists. It was a "brand new" solution for my own app. However, lucky you that you found a solution :) – Hans One Apr 13 '15 at 14:00