Using two two managed object contexts for two threads

Discussion in 'iPhone/iPad Programming' started by RookieAppler, Jan 29, 2013.

  1. macrumors member

    Joined:
    Mar 15, 2012
    #1
    I have two threads in my iOS program. One is the main thread. The other the background thread is launched to get data from web services as i dont want to disturb the UI. I read that i have to create two separate managed object contexts. So i did it like this
    Code:
    @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
    @property (strong, nonatomic) NSManagedObjectContext *secondmanagedObjectContext;
    
    In my background thread i used secondMOC. How do i merge them after my background thread is finished working? Can some one explain me. thanks.
     
  2. macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #2
    Have a look at Child-Parent Contexts in the core data documents. (iOS5+)
    If your second context on the background thread is a child of the first one. Then anytime you call save on that child context it pushes the changes to the parent context. You need to then save the parentContext to commit those changes to the persistent store. Gets tricky if the object might have been altered in both contexts, but if that not possible in your situation it's a lot easier.
     
  3. thread starter macrumors member

    Joined:
    Mar 15, 2012
    #3
    @MattInOz. Thanks. So i think its not parent child. Tell me this. I have the main thread. I perform login. I pull data from web service for that. Now if login is true i launch a background thread like this
    Code:
    				dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
    														 (unsigned long)NULL), ^(void) {
    					[self getAllCustomerValues];
    				});
    
    In the getAllCustomerValues i write like this:
    Code:
    -(void)getAllCustomerValues
    {
    ....
    	strPath =@"";//some valid URL
    	
    	ttbXML = [TBXML tbxmlWithURL:[NSURL URLWithString:strPath]];
    	if(ttbXML.rootXMLElement)
    	{
    		[self traverseElement:ttbXML.rootXMLElement];
    	}
    	else
    	{
    		NSLog(@"the else at 894");
    	}
    ....
    }
    
    - (void) traverseElement:(TBXMLElement *)element {
    self.secondMOC = self.appDelegate.managedObjectContext;
    
    ...
    ITMAllCustomerAddresses *allCustAddress =(ITMAllCustomerAddresses *)[NSEntityDescription insertNewObjectForEntityForName:@"ITMAllCustomerAddresses"	inManagedObjectContext:self.secondMOC];
    }
    
    So i think they are 2 separate MOCs. How do i go about doing it now? Hope it helps you understand my problem.Thanks
     
  4. MattInOz, Jan 30, 2013
    Last edited: Jan 30, 2013

    macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #4
    Nothing in that code suggests there are two ManagedObjectContext(MOC).
    As you have it self.secondMOC is just assigned reference to the appDelegate's ManagedObjectContext. It would be the same object and given how Apple's standard templet is That MOC is probably running on the main Thread.

    Have you read the Core Data Concurrency document?
    conceptual/CoreData/Articles/cdConcurrency.html

    Edit also good info here:
    core-data-and-threads-without-the-headache/
     

Share This Page