|
|
#1 |
|
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; |
|
|
|
0
|
|
|
#2 | |
|
Quote:
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" [13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13", iPhone 4, iPad] |
||
|
|
0
|
|
|
#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];
});
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];
}
|
|
|
|
0
|
|
|
#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/
__________________
There is no such thing as "Collective Wisdom" [13" MacBookPro 2.7Ghz, 27"Al iMac i7, Black MacBook 13", iPhone 4, iPad] Last edited by MattInOz; Jan 30, 2013 at 07:27 PM. |
|
|
|
0
|
![]() |
|
| Tags |
| background, ios, managedobjectcontext, nsthread, thread |
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 01:56 AM.








Linear Mode
