iphone - cancel a background thread in Objective-C -
how cancel background thread in objective-c?
i calling background threads so:
[self performselectorinbackground:@selector(setupthumbnails) withobject:nil];
it called when swipe happens, each swipe want cancel previous background thread request.
maintain cancellation property thread, , check periodically within thread. you'll need either use lock or atomic function (as provided operating system , frameworks, not implemented yourself!) indicate thread should cancelled. system-provided atomic functions documented in atomic
manpage.
even better use nsoperation on nsoperationqueue instead of nsthread. still perform work in background using thread, lets framework , operating system manage thread pool on behalf, , nsoperation has iscancelled
property can check - ability cancel either individual nsoperation or operations in entire nsoperationqueue.
for example, have "set 1 thumbnail" operation, , add 1 such operation queue each thumbnail set up. os , framework cooperate manage number of threads used execute operations, though set queue's width ensure @ many imports run @ once. , if user cancels, can ask queue cancel outstanding operations.
Comments
Post a Comment