Tag:ios delay execution
/* This article lists four ways to delay the execution of a function and some of its differences. If the delay is 1 seconds, execute the following method. */-(void) delaymethod{ NSLog (@ "execute");} /** * "1". Performselector method */[self Performselector: @selector (delaymethod) Withobject:nil afterdelay:1.0f];// This method requires that it must be executed in the main thread, otherwise it is not valid. is a non-blocking execution mode,//the method for canceling execution is not found temporarily. /** * "2". Timer: Nstimer */[nstimer scheduledtimerwithtimeinterval:1.0f target:self selector: @selector (Delaymethod Userinfo:nil repeats:no];//This method requires that it must be executed in the main thread, otherwise it is not valid. is a non-blocking way of execution,//Can be invalidate by Nstimer class-(void); /** * "3". Sleep mode */[nsthread sleepfortimeinterval:1.0f]; [Self delaymethod];//This method can be executed in both the main thread and the child thread. is a blocking method of execution, which is placed in a sub-thread to avoid jamming the interface//not finding a way to cancel execution. /** * "4". GCD mode */double delayinseconds = 1.0;__block viewcontroller* bself = self;dispatch_time_t Poptime = Dispatch_time (Dispatch _time_now, (int64_t) (Delayinseconds * nsec_per_sec));d ispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) { [Bself Delaymethod];}); /This way you can select the executing thread in the parameter. is a non-blocking way of execution,//the method of canceling execution is not found.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Several ways to delay execution of the iOS development series