2

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

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

下面是函数,它基本上计算距离和postes通知

-(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 [email protected]"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];  
    }  
} 
+0

是否有一个特定的方法来定义这个函数。 – IphoneBites

+2

NSLog(@“ho gaya bhai”); Abhi hua ki nahi? –

+0

Ho gaya bhai !! hAhahaha – IphoneBites

回答

3

多久是你的函数取来完成?你只有5秒钟的时间执行applicationDidEnterBackground:中的任务并返回。

从苹果公司的UIApplicationDelegate Protocol Reference

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

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

+0

我该如何检查函数的使用时间,也可以在这里应用一个计时器。 – IphoneBites

+0

@IphoneBites:你可以使用'NSTimer'大致检查它需要多长时间,并在控制台上打印持续时间。 – Stuart

+0

@IphoneBites:更好的是,在乐器中使用Time Profiler。 – Stuart

2

据我所知,不应该在applicationDidEnterBackground中调用任何耗时的函数,因为该应用程序将在短时间内暂停。

从苹果公司的IOS Programming Guide

是进入后台时,大多数应用程序移动到暂停状态之后不久。在此状态下,应用程序不会执行任何代码,并且可能随时从内存中移除。为用户提供特定服务的应用程序可以请求后台执行时间以提供这些服务。

GOOL好运:)

1

有你使用NSThread或做一些逻辑调用此方法

- (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. */} 

//这里面的方法尝试调用计算试过,例如位置方法可能会起作用(试试看这里)