Executing multiple NSOpeartion synchronously with NSOperationQueue
I have to download multiple files from Server in background process.
for this i am using NSOperationQueue
. During download i need to maintain when download has started & completed. For this i have been using Core Data.
I have been using following approach:
There is NSManagedObject
corresponding to each file.
- So before download is begin i save state for file in core data as "Started"
- NSOperation subclass is there.Instance is created for each file where i pass the managedObject instance .It is then added to
NSOperationQueue
. - Then in main() method of
NSOperation
subclass i do the actual file download . - When download is completed i save state for fiel in core data as "Completed".
this works fine for single file.But for multiple files i execute Step 1 to 4 in for loop .
This destroy NSMangedObject
passed to NSOperation
subclass for second iteration for for loop
This is how i calling it
for(NSManagedObject *objToDownload in objectArr){
NSManagedObjectContext *ctx = [[NSManagedObjectContext alloc] init];
[ctx setPersistentStoreCoordinator: [[[UIApplication sharedApplication] delegate] persistentStoreCoordinator]];
offlineManagerObj = [[OfflineFileMa开发者_如何学Pythonnager alloc]initWithManagedObj:objToDownload delegate:self tempOfflineAccessPath:[objToDownload valueForKey:@"path"] objContext:ctx];
[queue setMaxConcurrentOperationCount:1];
[queue addOperation:offlineManagerObj];
[ctx release];
}
I got following crash logs
#0 0x31b6b4b0 in ___forwarding___ ()
#1 0x31ae2180 in __forwarding_prep_0___ ()
#2 0x363b904e in -[_PFArray dealloc] ()
#3 0x363b6b80 in -[_PFArray release] ()
#4 0x31acd1a0 in CFRelease ()
#5 0x31acfeba in _CFAutoreleasePoolPop ()
#6 0x310ae1ca in -[NSAutoreleasePool release] ()
#7 0x370bff0e in _UIApplicationHandleEvent ()
#8 0x35bc0e76 in PurpleEventCallback ()
#9 0x31b3fa96 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#10 0x31b4183e in __CFRunLoopDoSource1 ()
#11 0x31b4260c in __CFRunLoopRun ()
#12 0x31ad2ec2 in CFRunLoopRunSpecific ()
#13 0x31ad2dca in CFRunLoopRunInMode ()
#14 0x35bc041e in GSEventRunModal ()
#15 0x35bc04ca in GSEventRun ()
#16 0x370ead68 in -[UIApplication _run] ()
#17 0x370e8806 in UIApplicationMain ()
#18 0x00002482 in main (argc=1, argv=0x2fdff494) at /Projects/iOS_Universal/main.m:14
I even tried setting NSZombieEnabled
but that too didnt help me to get which object is over-released
You should allocate the context outside the loop, and release it after.
Also if you want objects to persist, you need to save
the context.
精彩评论