Reputation: 2809
Is there a way to delay a method call before it changes a key value in the user defaults?
For instance; I have method A which is an IBAction
and Method B. If a key "keyOne" is false; method A sets "keyOne" to true via the -[NSUserDefaults setBool: forKey:]
and then calls method B with an integer input of for time delay. Method B then needs to wait for whatever the delay was input to be in seconds and then change the "keyOne" back to true with the same NSUserDefaults.
Upvotes: 2
Views: 914
Reputation: 130222
Use GCD's dispatch_after()
to delay the operation. But, instead of using the main queue as generated by the Xcode code snippet, create your own queue, or utilize the background queue.
dispatch_queue_t myQueue = dispatch_queue_create("com.my.cool.new.queue", DISPATCH_QUEUE_SERIAL);
double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, myQueue, ^(void){
// Reset stuff here (this happens on a non-main thead)
dispatch_async(dispatch_get_main_queue(), ^{
// Make UI updates here. (UI updates must be done on the main thread)
});
});
You can find more information on the difference between using performSelector:
and dispatch_after()
in the answer in this post: What are the tradeoffs between performSelector:withObject:afterDelay: and dispatch_after
Upvotes: 9
Reputation: 25459
You can use perform selector from method A to call method B:
[self performSelector:@selector(methodName) withObject:nil afterDelay:1.0];
If your method B needs to know the delay you can use withObject: to pass parameter, it needs to be NSNumber, not integer.
Upvotes: 3