Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When using UIManagedDocument, you do not call save on the MOC because it implements auto-save. however, it needs to be told that an auto-save should take place at some point in the future.</p> <ol> <li><p>Get rid of that call to openWithCompletionHandler in that function (I know it was just there for purposes of debugging this problem).</p></li> <li><p>Replace</p> <p>[managedDocument.managedObjectContext save:&amp;InternalError ]</p></li> </ol> <p>with</p> <pre><code>[managedDocument updateChangeCount:UIDocumentChangeDone]; </code></pre> <p>This will notify the document that it can now be saved.</p> <p><strong>EDIT</strong></p> <p>First, I think you should get rid of the debugging hacks. You can add NSLog or NSAssert, but the rest of that stuff just makes it hard to tell why you want, and confuses the real issue.</p> <p>Second, what is your real goal here? I can see the name of the method, and I can see the code, but they do not match.</p> <p>There is so much "cruft" here, it is hard to understand your problem. I am going to repost your code, along with an edit to remove the "open" stuff, and annotate it with questions as code comments.</p> <p>Hopefully, this change will help you solve your problem.</p> <pre><code>// First, the method name seems to indicate that some objects will be added // to the database. however, the only database work in this method is removal. // I don't get it. + (void ) saveCalendarArrayInDbIfItAlreadyDoesNotExist : (NSArray*) appCalendarArray managedDocument: (UIManagedDocument*) managedDocument { NSError *error = nil; NSMutableArray *array = [[NSMutableArray alloc] init]; for (id appCalendar in appCalendarArray) { if([appCalendar isKindOfClass:[AppCalendarEntity class]]) { // OK, we are filtering the array of objects. We are only interested in // objects of type AppCalendarEntity, and are going to use its identity // property to look for objects of type MyEntity. // What is the relationship between AppCalendarEntity and MyEntity? AppCalendarEntity *appCalendarEntity = (AppCalendarEntity*) appCalendar; NSFetchRequest *requestToSeeIfCalendarWithIdExist = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"]; requestToSeeIfCalendarWithIdExist.predicate = [NSPredicate predicateWithFormat:@"identifier = %@", appCalendarEntity.identifier]; NSError *InternalError = nil; [requestToSeeIfCalendarWithIdExist setShouldRefreshRefetchedObjects:YES]; NSArray *result = [managedDocument.managedObjectContext executeFetchRequest:requestToSeeIfCalendarWithIdExist error:&amp;InternalError]; // OK, now we just got a result from searching for a MyEntity, where // its identifier is the same as the appCalendarEntity. if(result == nil) { // return error } // 1 object always return that depict the in memory(context) object we created but not saved. I expect it should be zero because no object has yet been saved to database.. else if(result.count &gt; 1) { // I am extremely confused by this code. First, why are you // checking for more than 1 object? The method name indicates // you are going to insert something. Furthermore, you are only // deleting one object. How many do you expect? Also, why are // you deleting an appCalendar? You were searching for a MyEntity. // If an appCalendar is a MyEntity, then that's terrible naming. // Furthermore, it would explain why you are finding it... // because you create entities by inserting them in a MOC to // begin with! [managedDocument.managedObjectContext deleteObject:appCalendar]; } else { // Even more confusion. You are adding this object to an internal // array, not the database. Furthermore, you are doing it if there // are either 0 or 1 MyEntity objects in the database with matching // identifier. [array addObject:appCalendarEntity]; } } } // saving all the objects // OK - but the only thing being saved are the ones you deleted... [ managedDocument updateChangeCount:UIDocumentChangeDone ]; } </code></pre> <p>Finally, if my hunch is correct, and the calendar objects are actually MyEntity objects, they are already in the MOC - because that's how they get created. When you do a fetch, you can force the search to ignore pending changes (as noted in one of my previous comments) and only accept saved changes.</p> <p>If you want to ignore pending changes, </p> <pre><code>fetchRequest.includesPendingChanges = NO; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload