0
votes

I am trying to fetch Request from my DB. but i am getting nil in the response. The fetch is ok because when i use this method i am getting "sometimes" Values. (i know that i have data in the DB ). i think that this is somehow related to threads issue or because i am using Multi-Context. So my Question is basically generic.

  1. if i am saving some data in specific MOC let's call it MOC1 and then i want to fetch that data that i just save .. i can use any MOC ? ( assuming that i save the child and parent and i can see the change on the DB )

  2. if i save data with MOC that use NSPrivateQueueConcurrencyType can i fetch with MOC NSMainQueueConcurrencyType? and the opposite way.. ?

  3. i can fetch data on background thread or i have to fetch on the main Thread?

  4. what is the connection between the Main thread and NSMainQueueConcurrencyType

  5. what is the connection between the Background thread and NSPrivateQueueConcurrencyType Thanks,

UPDATE:

The thing is that i fetch some entity and change some flag to isSync = YES when i want to save it i use my save method:

-(void)saveDataToDBWithCompletionWithManagedObjectContext:(NSManagedObjectContext*)managedObjectContext withCompletion:(void (^)(BOOL succeeded ,NSError *error))completion {
if (managedObjectContext != nil) {
    [managedObjectContext performBlockAndWait:^{
        NSError *errorMsg = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&errorMsg]) {
            // do some real error handling
            NSLog(@"couldn't save Background Context, error %@, %@", errorMsg, [errorMsg userInfo]);
            if (completion) {
                completion(NO, errorMsg);
            }
        } else {
            if (completion) {
                completion(YES, nil);
            }
        }
    }];
} else {
    if (completion) {
        completion(NO, nil);
    }
}

}

This method get the NSManagedObjectContext, and save it in to the relevant NSManagedObjectContext. then i run my master save:

-(void)saveMasterDataToDBWithSync:(BOOL)toDoSync WithCompletion:(void (^)(BOOL succeeded ,NSError *error))completion {
NSError *errorMsg = nil;
if (self.masterManagedObjectContext != nil) {
    if ([self.masterManagedObjectContext hasChanges] && ![self.masterManagedObjectContext save:&errorMsg]) {
        if (completion) {
            completion(NO, errorMsg);
        }
    } else {
        if (toDoSync) {
          // DO sync with Parse
        }

        if (completion) {
            completion(YES, nil);
        }
    }
} else {
    if (completion) {
        completion(NO, nil);
    }
}

}

When i check my DB the data is there! but when i will try to fetch it i can not see any thing.

1

1 Answers

1
votes
  1. Yes. But you need to find a way to notify that MOC about the changes you made. You can do that by using a child/parent context (parentContext), by saving to the persistentStoreCoordinator or by merging changes using mergeChangesFromContextDidSaveNotification. In any case you must save for the changes to propagate.

  2. Yes. Same as #1.

  3. You can fetch on whichever thread you wish as long as you use the thread/queue associated with this MOC. You can do that by using performBlock or just making sure your running a NSMainQueueConcurrencyType and calling the fetch from the main thread.

  4. NSMainQueueConcurrencyType should run only on the main thread.

  5. NSPrivateQueueConcurrencyType has its own private queue. Use performBlock to run the fetch on that queue.