开发者

Executing multiple NSOpeartion synchronously with NSOperationQueue

开发者 https://www.devze.com 2023-04-12 20:01 出处:网络
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

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.

  1. So before download is begin i save state for file in core data as "Started"
  2. NSOperation subclass is there.Instance is created for each file where i pass the managedObject instance .It is then added to NSOperationQueue.
  3. Then in main() method of NSOperation subclass i do the actual file download .
  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号