gpt4 book ai didi

iphone - 如何调用applicationDidEnterBackground中的函数?

转载 作者:行者123 更新时间:2023-12-03 20:28:24 25 4
gpt4 key购买 nike

我想调用applicationDidEnterBackground中的函数,该函数是在其他 Controller 中定义的。

我已经创建了一个对象来访问它,但似乎该函数在调用时被杀死。

这是一个主要计算距离并发布通知的函数

 -(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];
}
}

最佳答案

您的函数需要多长时间才能完成?您只有 5 秒的时间来执行 applicationDidEnterBackground: 中的任务并返回。

来自苹果的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.

关于iphone - 如何调用applicationDidEnterBackground中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7443131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com