2014-09-20 66 views
8

我的目標iOS 7上的C應用程序從startUpdatingsignificantLocationChanges或startUpdatingLocation委託(取決於應用程序所在的模式,但我認爲不重要)在後臺獲取位置更新。Firebase可以在iOS 7的後臺發送和接收嗎?

在委託中,我收集位置信息,將其寫入字典,然後將字典寫入Firebase。

// this code is in the location update delegate routine 

// the code that gathers the various elements that go into the dictionary 
// are omitted for clarity, I don't think that they matter 

// we may be running in the background on iOS 7 when we are called! 

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
    [[NSNumber numberWithFloat:newLocation.coordinate.latitude] stringValue], @"Latitude", 
    [[NSNumber numberWithFloat:newLocation.coordinate.longitude] stringValue], @"Longitude", 
    [[NSNumber numberWithFloat:newLocation.horizontalAccuracy] stringValue], @"Accuracy", 
    formattedDateString, @"TimeNow", 
    [dateFormatter stringFromDate:newLocation.timestamp], @"TimeStamp", 
    [[NSNumber numberWithDouble:interval] stringValue], @"Date", 
    self.mode, @"Mode", 
    nil]; 

    // Write it once to CurrentLocation 
    [ref setValue:dictionary]; 

    // yes, I know this is clumsy 
    fbTmp = [NSMutableString stringWithString: fbRoot]; 
    [fbTmp appendString : @"/locationHistory"]; 
    ref = [[Firebase alloc] initWithUrl:fbTmp]; 

    // Now write it again to the locationHistory list 
    ref = [ref childByAutoId]; 
    [ref setValue:dictionary]; 

有時工作,有時不(即應用程序的相同的運行,有時位置被寫入到火力地堡成功預期,並且有時沒有,而且也沒有任何明顯的韻律或理由,當它似乎工作,當它不)。

我懷疑問題是Firebase寫入在後臺模式下沒有成功完成,但我不確定。我對iOS和Objective C以及Firebase都很陌生。

我的應用程序在其功能中標記爲需要位置更新和後臺提取的後臺服務(後者是我隨機嘗試解決此問題,前者我知道我需要)。

我懷疑我需要告訴操作系統我需要時間用backkgroundTask完成寫入操作,然後在firebase寫入的完成塊中終止後臺任務 - 任何人都會驗證在運行時會起作用在後臺模式?

如果是這樣,我只需要在第二次寫入時(假設它們按順序完成)或兩者都有(在每次寫入完成時計數器計數)?

任何提示最受讚賞。

+1

應用程序可以在後臺執行哪些操作,而不是Firebase的權限。它保持連接一段任意長度,然後OS根據需要切斷套接字連接。這可能說明了「有時候有作品」。 – Kato 2014-09-24 15:20:03

回答

0

是的,你需要在後臺完成你的任務。它說,所以在Apple Documentation

如果您的應用程序是在任務執行過程中,需要一些額外的時間來完成這項任務,它可以調用beginBackgroundTaskWithName:expirationHandler:或beginBackgroundTaskWithExpirationHandler:在UIApplication對象的方法請求一些額外的執行時間。調用其中任何一種方法都會暫時延遲您的應用程序的暫停,給它一點額外的時間來完成其工作。完成該工作後,您的應用程序必須調用endBackgroundTask:方法來讓系統知道它已完成並可以暫停。

只是把你的代碼放在後臺任務中,給它最大的時間(3分鐘我想)。

請閱讀Apple app lifecycle,一切都應該清除以備將來參考。

相關問題