Search code examples
objective-ciosmultithreadingnsnotifications

how can we use combination of NSThread and NSNotification?


I am doing an application where images from the user are taken all together and saved in NSMutableArray. As soon as even one image has been start coming, I need to upload images to server one by one though images are taken together

I am using [NSThread detachNewThreadSelector:@selector(uploading:) toTarget:self withObject:imagearray]; to upload images one by one. I need to show progressview to user as images are being uploaded one by one.

How do I notify after one image has been uploaded?

Or is there any other scenario that is useful for this more than NSThread+NSNotification?


Solution

  • I sugest to use something similar to "delegate" paradigm but thinking on threads instead of objects. So the uploading thread delegates on main thread as it is the one to make user interface changes. For example, the uploading thread can send messages for partial upload progress

    [self performSelectorOnMainThread:@selector(uploadProgression:) 
            withObject:foo waitUntilDone:NO]
    

    or for each complete upload finished

    [self performSelectorOnMainThread:@selector(uploadDidEnd:) withObject:foo
            waitUntilDone:YES]
    

    I suppose that you have not to stop uploading for updating partial progress in progessView but you need to wait when upload ends so not to duplicate uploading threads launching a new upload.