如何在applicationDidEnterBackground中调用函数? [英] How to call a function in applicationDidEnterBackground?

查看:135
本文介绍了如何在applicationDidEnterBackground中调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在中调用一个函数applicationDidEnterBackground 这个函数在其他控制器中定义。

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


推荐答案

如何你的功能是完整的吗?您只有5秒钟在 applicationDidEnterBackground:中执行任务并返回。

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

来自Apple的 UIApplicationDelegate协议参考


这个方法的实现大约需要5秒到
执行任何任务并返回。如果您需要额外的时间来执行
任何最终任务,您可以通过调用beginBackgroundTaskWithExpirationHandler:从
系统请求额外的执行时间。在
练习中,您应该尽快从applicationDidEnterBackground:
返回。如果方法在时间运行之前没有返回
out你的应用程序终止并从内存中清除。

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.

你应该执行与调整用户界面相关的任何任务
在此方法退出之前,但其他任务(例如保存状态)应根据需要将
移动到并发调度队列或辅助线程。
因为你可能在
applicationDidEnterBackground中启动任何后台任务:直到
退出该方法之后才会运行,你应该在
启动这些任务之前请求额外的后台执行时间。换句话说,首先调用
beginBackgroundTaskWithExpirationHandler:然后在
调度队列或辅助线程上运行任务。

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.

这篇关于如何在applicationDidEnterBackground中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆