2016-09-29 2 views
0

Hallo Ich speichere meine Daten mit der saveContext(), die mit der AppDelegate-Vorlage generiert wird. Meine App zeichnet im Hintergrundmodus Gruppen von Speicherorten auf, und wenn die App in den Vordergrund wechselt, nehme ich diese Speicherorte und speichere sie in den Kerndaten. Alles wird gespeichert und gespeichert, aber wenn ich zu meinem Ansichts-Controller gehe, wo ich sie zeige, wird es nicht angezeigt, wenn ich die App nicht erneut starte und zurückkomme.CoreData wird nur aktualisiert, nachdem ich die App neu gestartet habe?

func applicationDidBecomeActive(_ application: UIApplication) { 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    if self.myLocations.count > 0 { 

     let context = persistentContainer.viewContext 
     let logEntity = NSEntityDescription.insertNewObject(forEntityName: "PostureLog", into: context) 

     logEntity.setValue(self.errors, forKey: "logError") 

     // populate log 
     logEntity.setValue(Date(), forKey: "logDate") 

     for i in 0...myLocations.count - 1 { 
      let locationEntity = NSEntityDescription.insertNewObject(forEntityName: "Location", into: context) 

      // populate address 
      locationEntity.setValue(myLocations[i].coordinate.latitude, forKey: "latitude") 
      locationEntity.setValue(myLocations[i].coordinate.longitude, forKey: "longitude") 

      // create relationship Location -> Log 
      locationEntity.mutableSetValue(forKey: "log").add(logEntity) 

     } 

     self.saveContext() 
     self.myLocations.removeAll() 
     self.errors = 0 


    } 
} 

speichern Kontextfunktion

func saveContext() { 
    let context = persistentContainer.viewContext 
    if context.hasChanges { 
     do { 
      try context.save() 
     } catch { 
      // Replace this implementation with code to handle the error appropriately. 
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      let nserror = error as NSError 
      fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 
     } 
    } 
} 

Antwort

0

Sie sind wahrscheinlich nicht die Viewcontroller erfrischend, so dass es immer noch die alten Daten, die zeigen, bis Sie neu starten.

Verwandte Themen