Note that there are some explanatory texts on larger screens.

plurals
  1. POUnexpected behavior of NSFetchRequest
    primarykey
    data
    text
    <p>I have created few entities in context for saving it in db using </p> <pre><code>AppCalendarEntity *appCalendar = [AppCalendarEntity getInstanceWithManagedDocument:manageDocument]; </code></pre> <p>After adding a few entities I execute flowing fetch request</p> <pre><code> NSFetchRequest *requestToSeeIfCalendarWithIdExist = [NSFetchRequest fetchRequestWithEntityName:@"AppCalendarEntity"]; NSArray *result = [managedDocument.managedObjectContext executeFetchRequest:requestToSeeIfCalendarWithIdExist error:&amp;InternalError] ; </code></pre> <p>It returns me the result including only the entities I have added in context using first command and NOT the entries already saved in database. I have made sure that at this stage the document state is UIDocumentStateNormal.</p> <p>When I add this line to already open document (UIDocumentStateNormal) it returns me the expected result, i.e. it fetch results from db as well as memory context which has not yet been saved to db.</p> <pre><code>[managedDocument openWithCompletionHandler:^(BOOL success) { NSFetchRequest *requestToSeeIfCalendarWithIdExist = [NSFetchRequest fetchRequestWithEntityName:@"AppCalendarEntity"]; NSArray *result = [managedDocument.managedObjectContext executeFetchRequest:requestToSeeIfCalendarWithIdExist error:&amp;InternalError] ; } </code></pre> <p>My question is 1- I expect that the result of query should be the same in both cases. Why it is not so in the above case. 2- To me if document state is UIDocumentStateNormal I should not be calling "openWithCompletionHandler" in context to open the document. In this particular scenario what difference it is making in NSFetchRequest which gives me the desired result after adding this.</p> <p>Please let me know if I'm getting wrong</p> <p>Here is the complete code</p> <p>This is the complete code of the function </p> <pre><code> + (void ) saveCalendarArrayInDbIfItAlreadyDoesNotExist : (NSArray*) appCalendarArray managedDocument: (UIManagedDocument*) managedDocument completionBlock : ( void(^) (NSArray* ObjectSavedSuccesfully, NSError *InternalError)) handler { // i dont know why i have to do it :( if i dont add openWithCompletionHandler my query doesnt fetch result from db rather just do query in-memory context and not db [managedDocument openWithCompletionHandler:^(BOOL success) { void (^completionHandler)(NSArray* , NSError* ); completionHandler = [handler copy ]; NSError *error = nil; NSMutableArray *array = [[NSMutableArray alloc] init]; for (id appCalendar in appCalendarArray) { if([appCalendar isKindOfClass:[AppCalendarEntity class]]) { 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] ; // "result" is different when we encapsulate it in openWithCompletionHandler and when we don't…….MY PROBLEM 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) { [managedDocument.managedObjectContext deleteObject:appCalendar]; } else { [array addObject:appCalendarEntity]; } } else { // error handling } } if (error != nil) { completionHandler (nil, error); return; } // saving all the objects [ managedDocument updateChangeCount:UIDocumentChangeDone ]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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