grand-central-dispatch

cancel dispatch_after() method? [duplicate]

江枫思渺然 提交于 2019-11-27 15:35:07
问题 This question already has answers here : Prevent dispatch_after() background task from being executed (10 answers) Closed 4 years ago . Is there a way to cancel dispatch_after() scheduled for some time in future, and haven't fired so far? I'm trying to make something like a scheduler for updates from server, and this method is just like I want, but, I'd love to cancel and re-schedule it at some point. Is it possible at all or I have to fallback and use NSTimer? 回答1: There is NO way to prevent

How do I kill/suspend/close an asyncronous block in GCD?

自闭症网瘾萝莉.ら 提交于 2019-11-27 15:25:53
问题 I've implemented a block that is dispatched asynchronously using GCD as follows: __block BOOL retValue; dispatch_async(dispatch_get_global_queue(0, 0), ^{ retValue = [self GCDHandler:actionName WithServiceType:serviceType :arguments]; }); return retValue; How do I cancel such a block if it is running for longer than I would like? Is there a way to cancel GCD-dispatched blocks, or provide a timeout to them? 回答1: There is no built in way to cancel GCD blocks. They're rather set and forget. One

Block_release deallocating UI objects on a background thread

百般思念 提交于 2019-11-27 15:01:01
One of the patterns presented at the WWDC 2010 "Blocks and Grand Central Dispatch" talk was to use nested dispatch_async calls to perform time consuming tasks on a background thread and then update the UI on the main thread once the task is complete dispatch_async(backgroundQueue, ^{ // do something time consuming in background NSArray *results = ComputeBigKnarlyThingThatWouldBlockForAWhile(); // use results on the main thread dispatch_async(dispatch_get_main_queue(), ^{ [myViewController UpdateUiWithResults:results]; }); }); Since "myViewController" is being used inside the blocks, it

kill items in a dispatch_async queue in iOS

此生再无相见时 提交于 2019-11-27 14:27:06
I am running a bunch of items in the background using dispatch_async and sometimes I want to kill what I have in the queue - is this possible? For instance this code is run on a view, and then the user goes back a screen. All of these fired actions keep running regardless of the back navigation. Ideally I would like to kill these items from running: dispatch_async(dispatch_get_global_queue(2, 0), ^{ for (int i=0; i<[self.manufacturers count]; i++) { NSString *manufacturerID = [[[self.manufacturers objectAtIndex:i] ManufacturerID] stringValue]; [self doSync:manufacturerID withIndex:i setTimer

Prevent dispatch_after() background task from being executed

夙愿已清 提交于 2019-11-27 14:25:52
This is my issue. When my application enters background I want it to perform a function after certain period of time. This is what I do: - (void)applicationDidEnterBackground:(UIApplication *)application { isRunningInBackground = YES; taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]; int64_t delayInSeconds = 30; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { [self doSomething]; }); } - (void)doSomething {

Do 2 objects creating serial queues with the same name share the same queue

a 夏天 提交于 2019-11-27 13:41:21
问题 Not sure on the behavior, because I suspect I am getting a deadlock, I have a class with multiple objects - each object creates a queue with the same name. I'm not sure if GCD is reusing the same queue between the objects or if they just share the same name. For instance @interface MyClass -(void)doSomeWork @property (nonatomic,strong) dispatch_queue_t myQueue; @end @implementation MyClass -(id)init { self = [super init]; self.myQueue = dispatch_queue_create("MyQueue",DISPATCH_QUEUE_SERIAL);

NSOperationqueue background, download images

醉酒当歌 提交于 2019-11-27 13:18:40
问题 I created a NSOperationQueue to download images (from Twitter for Cell): NSOperationQueue *queue = [[NSOperationQueue alloc]init]; [queue addOperationWithBlock:^{ NSString *ImagesUrl = [[NSString alloc]initWithFormat:@"http://api.twitter.com/1/users/profile_image/%@",[[status objectForKey:@"user"]objectForKey:@"screen_name"]];; NSURL *imageurl = [NSURL URLWithString:ImagesUrl]; UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]]; [[NSOperationQueue mainQueue

What does main.sync in global().async mean?

爷,独闯天下 提交于 2019-11-27 13:17:25
In Swift, I used this kind of pattern sometimes. DispatchQueue.global().async { // do stuff in background, concurrent thread DispatchQueue.main.sync { // update UI } } The purpose of this pattern is clear. Do time consuming calculation in global thread so UI is not locked and update UI in main thread after calculation is done. What if there's nothing to calculate? I just found a logic in my project which //A DispatchQueue.main.sync { // do something } crashes but // B DispatchQueue.global().async { DispatchQueue.main.sync { // do something } } doesn't crash. How are they different? And Is case

Keep blocks inside a dictionary

最后都变了- 提交于 2019-11-27 13:07:49
问题 I have my own method that takes a block as an argument. I want to keep track of that block inside an NSDictionary. What is the best way to add the block to the dictionary? I tried this code but after executing the line below (setObject...) the dictionary is still empty. I presume that is because the block is not of type NSObject. But what is the right way to do this? - (void)startSomething:(NSURLRequest*)request block:(void (^)(NSURLResponse*, NSData*, NSError*))handler { NSURLConnection

How to asynchronously load an image in an UIImageView?

孤街醉人 提交于 2019-11-27 11:42:06
I have an UIView with an UIImageView subview. I need to load an image in the UIImageView without blocking the UI. The blocking call seems to be: UIImage imageNamed: . Here is what I thought solved this problem: -(void)updateImageViewContent { dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ UIImage * img = [UIImage imageNamed:@"background.jpg"]; dispatch_sync(dispatch_get_main_queue(), ^{ [[self imageView] setImage:img]; }); }); } The image is small (150x100). However the UI is still blocked when loading the image. What am I missing ? Here is a small code