How to call a function in applicationDidEnterBackg

2020-03-26 13:23发布

I want to call a function in applicationDidEnterBackground this function is defined in other controller.

I have made an object to access it but it seems that function is getting killed when called.

Here is the function it basically calculates the distance and postes a notification

 -(void)calculateDistance
{

    for (NSMutableDictionary *obj in placeName) {
        CLLocation *userLocation = [[AppHelper appDelegate] mLatestLocation];
        CLLocation *annotation1 = [[CLLocation alloc] initWithLatitude:[[obj objectForKey:@"Lat"]doubleValue] longitude:[[obj objectForKey:@"long"]doubleValue]];

        CGFloat distanceTemp = [annotation1 getDistanceFrom:userLocation];

        [obj setObject:[NSNumber numberWithFloat:distanceTemp] forKey:@"distance"];
        [annotation1 release];
    }


    if ([placeName count])
    {
        NSArray *sortedArray=[placeName sortedArrayUsingFunction:intSort context:NULL];
        self.placeName = [NSMutableArray arrayWithArray:sortedArray];
        NSMutableArray *arrayTemp = [[NSMutableArray alloc] initWithArray:placeName];


        for (int i =0; i < [placeName count]; i++) 
        {
            //                  NSArray *sortedArray=[placeName sortedArrayUsingFunction:intSort context:NULL];

            NSMutableArray *tempArray = [sortedArray objectAtIndex:i];
            //DLog(@"sortedArray%@", sortedArray);8=
            NSNumber *DistanceNum = [tempArray objectForKey:@"distance"];
            NSLog(@"distance%@:::",DistanceNum);
            NSInteger intDistance = (int)[DistanceNum floatValue];

            if(intDistance<500)
            {
                NSLog(@"ho gaya bhai");
                NSString *notifications =@"Yes";
                [[AppHelper mDataManager] setObject:notifications forKey:@"notifications"]; 

                NSLog(@"notifications:%@",notifications);
                RemindMeViewController *object = [[RemindMeViewController alloc] initWithNibName:@"RemindMeViewController" bundle:nil];
                //  RemindMeViewController *object=[[RemindMeViewController alloc]initWithNibName];

                NSLog(@"notifications set");
                [object scheduleNotification];
            }
            else
            {
                // [arrayTemp removeObjectAtIndex:i];
            }   
        }

        //after for loop is ended
        self.placeName= arrayTemp;
        DLog(@"remaining",arrayTemp);

        [arrayTemp release];
        [mTableView reloadData];    
    }       
}

3条回答
爷、活的狠高调
2楼-- · 2020-03-26 13:37

How long is your function taking to complete? You only have 5 seconds to perform tasks in applicationDidEnterBackground: and return.

From Apple's UIApplicationDelegate Protocol Reference:

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your application is terminated and purged from memory.

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

查看更多
贪生不怕死
3楼-- · 2020-03-26 13:43

As far as I know you should not call any time consuming functions in applicationDidEnterBackground since the app will get suspended after a short amount of time.

From Apple's IOS Programming Guide

Most applications that enter the background state are moved to the suspended state shortly thereafter. While in this state, the application does not execute any code and may be removed from memory at any time. Applications that provide specific services to the user can request background execution time in order to provide those services.

Gool luck :)

查看更多
The star\"
4楼-- · 2020-03-26 13:53

have you tried this, for example using a NSThread or make some logic to call this method

- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
 Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
 If your application supports background execution, called instead of applicationWillTerminate: when the user quits. */}

// inside this method try to call the calculate position method may be it will works(try nsthread here)

查看更多
登录 后发表回答