Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 29, 2013, 12:47 PM   #1
RookieAppler
macrumors member
 
Join Date: Mar 2012
Using two two managed object contexts for two threads

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.
RookieAppler is offline   0 Reply With Quote
Old Jan 29, 2013, 01:51 PM   #2
MattInOz
macrumors 68030
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: Sydney
Quote:
Originally Posted by RookieAppler View Post
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.
__________________
There is no such thing as "Collective Wisdom"
[ iPhone 5s, iPad Mini, 13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13"]
MattInOz is offline   0 Reply With Quote
Old Jan 29, 2013, 03:13 PM   #3
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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
RookieAppler is offline   0 Reply With Quote
Old Jan 30, 2013, 06:55 PM   #4
MattInOz
macrumors 68030
 
MattInOz's Avatar
 
Join Date: Jan 2006
Location: 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/
__________________
There is no such thing as "Collective Wisdom"
[ iPhone 5s, iPad Mini, 13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13"]

Last edited by MattInOz; Jan 30, 2013 at 07:27 PM.
MattInOz is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
background, ios, managedobjectcontext, nsthread, thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: How do I convert an NSData object to an NSDictionary object, and vice versa? moonman239 iPhone/iPad Programming 6 May 2, 2014 01:17 PM
How do I access a child class's reference to a managed object? moonman239 iPhone/iPad Programming 2 Nov 6, 2013 05:40 PM
Missing threads from subscribed threads under User CP ddublu Site and Forum Feedback 2 Sep 23, 2013 10:31 AM
Managed to get one... Bahroo iPhone 6 Oct 10, 2012 06:00 PM

Forum Jump

All times are GMT -5. The time now is 07:25 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC