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

//這裏面的方法嘗試調用計算試過,例如位置方法可能會起作用(試試看這裏)