I have an app that needs to synchronize multiple entity types with a backend using an API.
To do that I created a subclass of NSOperation for each HTTP method (GET/POST/PUT/DELETE => 4 operations). For a given entity these operations have to be run in a specific order, so I created a 5th operation subclass which contains the synchronization sequence (basically an operation that creates multiple GET/POST/PUT/DELETE operations in the appropriate order and adds these operations to a queue created by the operation.).
Since i want to be able to synchronize multiple kind of entities (for example 'Users' / 'Events' / 'Tasks') at the same time depending on the connection of the user and with dependencies between some types of entities (for example i need to finish the 'Users' synchronization before i can start concurrently the 'Events' and 'Tasks' synchronizations) I use a NSOperationQueue that contains only NSOperations of type synchronization.
So just to sum up, if I made 1 change of each type (1 create, 1 update, ...) on each type of object, and that i want to synchronize everything. I'll have 1 NSOperationQueue that will contain 3 NSOperations (1 for each type of object), and each NSOperation will have its own NSOperationQueue that should run 4 NSOperation (1 for each change type).
Now comes the problems :
1st/ The "sub-operations" are using async NSURLConnection, and I don't know how should the NSURLConnection be configured ?
Should i use something like this :
connection = [[NSURLConnection alloc] initWithRequest: theRequest delegate: self startImmediately: NO];
NSRunLoop * currentRunLoop = [NSRunLoop currentRunLoop];
[currentRunLoop addPort: [NSPort port] forMode: NSDefaultRunLoopMode];
[connection scheduleInRunLoop: currentRunLoop forMode: NSDefaultRunLoopMode];
[connection start];
[currentRunLoop run];
Or should i use that :
while (!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
2nd/ At the moment i am overriding start for the operations because i want to be able to start them without using an NSOperationQueue when I have only 1 operation. Is it correct ? If yes, are there examples somewhere of start method implementations ?
3nd/ About the runloops and threads, each operation will always get a different runloop and thread ? And this thread / runloop management must be handled in the start method if i override it ?
4th/ is it a problem to use queue like I do (i think it's justified in my case) ?