When we do the countdown, we find that when your finger presses the screen, drag TableView slide, write on the cell Countdown stop Countdown, release continue Countdown. The study found that the Nstimer stopped when the drag TableView slipped.
This is actually Runloop's mode is doing strange.
Runloop can be understood as a message loop mechanism under cocoa, used to handle various message events, we do not need to manually create a runloop when developing, because the framework creates a default runloop for us, through the [Nsrunloop Currentrunloop] We can get a corresponding Runloop object under the current thread, but we need to be aware of the way messages are notified between different runloop.
Then the above topic, when opening a nstimer is essentially registering a new event source in the current Runloop, and when ScrollView scrolls, the current mainrunloop is in Uitrackingrunloopmode mode, In this mode, Nsdefaultrunloopmode messages are not processed (because Runloop mode is not the same), and to accept other Runloop messages while ScrollView scrolling, we need to change the runloopmode between the two. .
1 [[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
Simply said that Nstimer will not open a new process, just registered in the Runloop, runloop Each loop will detect the timer, see if it can be triggered. When Runloop is in a mode and the timer is registered in B mode, it is not possible to detect the timer, so it is necessary to register the Nstimer with a mode so that it can be detected.
A simple example, when creating a countdown to a nstimer, modifies the model:
- (ID) Initwithtimeout: (Nsinteger) Total {if(self =[self init]) {Self.counter=Total ; if(Total >0) {Self.countdown=YES; //Notice:scheduledtimerwithtimeinterval adds the timer to the current thread ' s run loopTimer = [Nstimer scheduledtimerwithtimeinterval:1target:self Sel Ector: @selector (countingdown:) userinfo:nil Repeats:yes]; [[Nsrunloop Mainrunloop] Addtimer:timer formode:nsrunloopcommonmodes]; returnSelf ; } } returnSelf ;}
In this case, in the HTTP asynchronous communication module, it is also possible to encounter such a problem, that is, in the asynchronous acquisition of image data to the server to notify the main thread to refresh the picture in TableView, when the TableView scroll is not stopped or the user's finger stays on the screen, the picture will not come out, Maybe it's the Runloop mode that's behind it.
Nstimer stop when swiping a page in iOS development