ios - Locking with Obj-C -
i'm getting confused how locking works in obj-c.
i have network request want call once @ same time. want block other calls until has received data.
my lock
of type nscondition
, static, _sending
bool. implementation. getwithsuccess
using afnetworking
perform action network requests, think can start multiple threads or using magic in gdc.
my problem implementation give me deadlocks, , need hint how correct implementation should work.
[lock lock]; while (_sending) [lock wait]; _sending = yes; [self getwithsuccess:^(nsurlrequest *request, nshttpurlresponse *response, id json) { _sending = no; [lock signal]; [lock unlock]; } fail:^() { _sending = no; [lock signal]; [lock unlock]; }];
i've accomplished similar using nsoperation
, nsoperationqueue
. depending on needs, can subclass nsoperation
, check if operation of type in queue before adding new 1 (to prevent duplicate calls) or put network requests in queue , execute in order, 1 @ time setting maxconcurrentoperationcount
1
you mentioned using afnetworking has nsoperation subclass called afurlconnectionoperation might suit needs well.
Comments
Post a Comment