IOS blog project construction-22-reminder digital supplement

Time:2021-11-25

In the previous section, the function of reminder number is completed, but there is still a problem. When we pull down or pull up the content of the home page, the reminder number at the bottom will not change. Because the main thread of the picture rotator is busy processing scrolling and has no time to calculate the time of the timer, it will not request the interface to obtain the reminder number, resulting in such a problem.

Look at the figure: from the print record, we can see that the timer will call the method of unread messages every two seconds, but when we scroll down the uiview, the timer will stop printing. The thread of the timer is in the main thread, and we should put it in the sub thread for processing.
IOS blog project construction-22-reminder digital supplement

1、 Add main thread to child thread

//Timing check no reading
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(checkUnreadCount) userInfo:nil repeats:YES];
    
    //Change timer to child thread
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

2、 The reminder number is displayed in the upper right corner of the app icon

IOS blog project construction-22-reminder digital supplement

What should we do here?
Calculate the number of all unread messages, and then only one method is needed to solve it:

//4. Set the number in the upper right corner of the icon
        [UIApplication sharedApplication].applicationIconBadgeNumber = result.count;

IOS blog project construction-22-reminder digital supplement

3、 Processing of program entering background sleep

Now, when the program enters the background, it will be in the sleep state by default. The timer will not send a request to obtain the unread reading. What should we do if we don’t need the program to sleep and continue to run?

Here you need to apply the proxy method to the application you learned earlier
IWAppDelegate.m

//  IWAppDelegate.m
//  ItcastWeibo
/**
 *Keep the program running in the background
 *1. Try to apply for background running time
 [application beginBackgroundTaskWithExpirationHandler:^{}
 
 *2. Declare your own application type in info.plist
 *
 */

/**
 *This method will be called when the app enters the background
 */
- (void)applicationDidEnterBackground:(UIApplication *)application
{
   //Start the task in the background to keep the program running (the time to keep running is uncertain)
    [application beginBackgroundTaskWithExpirationHandler:^{
        //Iwlog (@ "haha, continue running in the background --");
    }];
    
     //Local notification (regular reminder Pop-Up)
    // [UILocalNotification ];
}