0
votes

I'm uploading multiple photo's to a server with AFNetworking (POST). It works great, except on iPhone 4/4S where I run into memory issues. The problem is all the payloads are built in advance and fill up memory; request are build much faster then they are sent.

So rater than executing my AFNetworking calls serially, I need to wait for each call to: self.manager POST:parameters:success:failure: to complete before calling it again. My code is something like this:

iterate over a group of images
[self sendTheImageToTheServer:image completion:{ ok, I'm done. send me the next one now. }];
// sendTheImageToTheServer:image calls self.manager POST:parameters:success:failure:
done

Ideally I'd like to use a dispatch group so I can a be alerted when the last block is done.

Any suggestions would be great.

EDIT: I can't use setMaxConcurrentOperationCount to help with this because all my payloads are built in advance and fill up memory; I use AFNetworking constructingBodyWithBlock to construct the body of my POST. Also, I don't want this to block so I need to be notified when the last image is sent.

1

1 Answers

0
votes

Fixed. Here is what worked:

dispatch_queue_t queue;
queue = dispatch_queue_create("com.example.MyQueue", NULL);

for (FBPhoto *photo in page.images) {
   dispatch_async(queue, ^{
     @autoreleasepool {
        dispatch_semaphore_t sem = dispatch_semaphore_create(0);
           [self addPhotoToUploadQueue:photo callback:^{
             dispatch_semaphore_signal(sem);
            }];
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
     }
   });
}

This bit of code can be used to deterring when the last image is sent or rather the last item in the queue is processed:

dispatch_barrier_async(queue, ^{
    // all done
});