Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
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.
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
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.

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.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@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
 

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
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/
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.