Reputation: 7958
In my project, I have to post many data, one by one, to a remote server. I post one object by using the well known method:
[objectManager postObject:responseMapping
path:@"remoteMethod.json"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"OK WS RK:%@",mappingResult);
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Error WS RK:%@",error.localizedDescription);
}
];
Now, after entering the success block I should start a new query, and so on.
Which is the right method to do? Or rather, I came up with a technique: when entering success block, I post a notification to self
and go on.
My question is: is there other smarter or correct ways to handle it?
Upvotes: 0
Views: 63
Reputation: 14169
RestKit (i.e. the RKObjectManager
) already manages a queue of operations. Whenever you do a postObject:path:parameters:success:failure:
, an RKObjectRequestOperation
with a POST request for the given object is created, and enqueued to the manager’s operation queue. If you want to ensure the order of requests, set the concurrentRequestsLimit
to 1.
See the documentation for further discussion.
Upvotes: 0
Reputation: 119031
Notifications should be used when you're trying to notify unknown classes / instances about the occurrence of an event. That isn't what you're trying to do. It is more appropriate for you to call a method on self
in the success block and have that method generate the new request / take a request off a queue.
Your queue is most easily implemented using a mutable array. Any time you want to do something, add it to the array (perhaps in the form of an NSBlockOperation
). In the success
block, check if there is anything in the array and, if there is, start
and remove the first item.
When you add each item to the array you need to know if any operation is currently in progress. If not, you need to run the item added to (or instead of adding it) the queue. To do this, hold a BOOL
@property
, set it in each operation added to the queue and clear it in each success
block.
Upvotes: 1