Note that there are some explanatory texts on larger screens.

plurals
  1. PONSOperation in iOS - How to handle loops and nested NSOperation call to get Images
    primarykey
    data
    text
    <p>I need help understanding how to appropriate handle the following use case:</p> <p>Say I'm writing a Chat app:</p> <ol> <li>Launch App</li> <li>Ask server (<code>AFHTTPRequestOperation</code>) to give me a list of all new messages</li> <li>Loop through those messages to see if I need to download any images</li> <li>If yes, then make another call to the server (<code>AFImageRequestOperation</code>) to get the image</li> </ol> <p>I keep getting crashes where my managed object "message" is no longer in the same context, but I'm only using one <code>managedObjectContext</code>. </p> <p>Is it because of the way I'm nesting the calls, since they are asynchronous? I am almost positive the message isn't getting deleted elsewhere, because I see it.</p> <p>One thing to note is that is seems to happen when I change view controllers. I launch the app, and at the root view controller (RVC), it performs step #2 above. If I touch to go the the "<code>MessageListViewController</code>" (MLVC) before all of the images have downloaded, the associated messages for those images that didn't finish downloading suddenly have a nil <code>managedObjectContext</code>. </p> <p>Below is the relevant code:</p> <pre><code> AFHTTPRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestImageInfoURL success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSDictionary * JSONresponse = (NSDictionary *)JSON; if( [[[JSONresponse objectForKey:@"status"] uppercaseString] isEqualToString:@"ERROR"] ) { for( id&lt;CommsObserver&gt; observer in _observers ) [observer errorOccurred:[JSONresponse objectForKey:@"message"]]; } else { if( [JSONresponse containsKey:@"convoMessages"] ) { NSArray * messageList = [JSON objectForKey:@"messages"]; for( int i = 0 ; i &lt; messageList.count ; i++ ) { __block ConversationMessage * message = [JSONUtility convoMessageFromJSON:[messageList objectAtIndex:i]]; if( !message ) NSLog( @"Couldn't create the new message..." ); else { message.unread = [NSNumber numberWithBool:YES]; [[DataController sharedController] saveContext]; if( (!message.text || [message.text isEqualToString:@""]) &amp;&amp; (message.image || message.imageInfoKey) ) { NSString * imageURL = (message.image ? [Comms urlStringForImageInfo:message.image] : [Comms urlStringForImageKey:message.imageInfoKey extension:message.imageInfoExt]); NSURLRequest *requestImageURL = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]]; AFImageRequestOperation * imageOperation; imageOperation = [AFImageRequestOperation imageRequestOperationWithRequest:requestImageURL imageProcessingBlock:^UIImage *(UIImage *image) { return image; } success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { if( message.image ) { NSLog( @"updating imageInfo for message" ); [Utilities updateImageInfo:message.image withImage:image asPreview:YES asThumbnail:YES preserveSize:YES]; } else { NSLog( @"creating a new imageInfo for message" ); ImageInfo * imageInfo = [Utilities createImageInfoFromImage:image asPreview:NO asThumbnail:NO preserveSize:YES]; if( imageInfo.managedObjectContext == nil ) NSLog( @"imageInfo MOC is NIL" ); else if( message.managedObjectContext == nil ) { NSLog( @"message MOC is NIL" ); message = [[DataController sharedController] convoMessageForKey:message.key]; if( !message ) NSLog( @"message is NIL, meaning it wasn't found in the MOC" ); else if( !message.managedObjectContext ) NSLog( @"message MOC was STILL NIL" ); else NSLog( @"problem solved..." ); } if( imageInfo ) [[DataController sharedController] associateImageInfo:imageInfo withMessage:message]; } [[DataController sharedController] saveContext]; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { NSLog( @"Image DOWNLOAD error... \n%@" , [NSString stringWithFormat:@"%@" , error] ); }]; [imageOperation start]; } for( id&lt;CommsObserver&gt; observer in _observers ) [observer newConvoMessages:@[message.key]]; } } // End for loop of messageList } // End if JSONresponse } // End outer if ERROR statement } // End Success failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog( @"Error: \n%@" , [NSString stringWithFormat:@"%@" , error] ); } ]; [operation start]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload