I'm building an app that stores locations in core data when the server connection is no longer available, then pushes these locations to the server when a connection is restored.
After fetching and printing these fetched objects from core data, I get tons of faults initially. I understand faults and what they represent, but shouldn't the number of faults be the same number of objects? The number of faults heavily outnumbers the number of objects (I can print out a 100+ faults for only 10 Location entity array objects of {longitude, latitude, timestamp}).
I was hoping someone could advise me on what I'm doing wrong or if this is usual behavior. Much appreciated.
AppDelegate.swift calls fetch and prints core data objects when server returns to connected
var fetchedCoreData = self.coreDataManagement.fetchLog()
for dataPoints in fetchedCoreData
{
println(dataPoints)
}
`CoreDataManager.swift handles storing/fetching core data of Location entity
var locations = [NSManagedObject]()
//coordinates passed from AppDelegate
func saveCoords(latCoord: String, longCoord: String, timeCoord: String) {
let appDelegate =
UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
let entity = NSEntityDescription.entityForName("Location",
inManagedObjectContext:
managedContext)
let coordsInfo = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext:managedContext)
coordsInfo.setValue(latCoord, forKey: "latitude")
coordsInfo.setValue(longCoord, forKey: "longitude")
coordsInfo.setValue(timeCoord, forKey: "timestamp")
var error: NSError?
if !managedContext.save(&error) {
println("Could not save \(error), \(error?.userInfo)")
}
locations.append(coordsInfo)
}
`
//fetch core data objects
func fetchLog() -> Array<AnyObject> {
let appDelegate =
UIApplication.sharedApplication().delegate as AppDelegate
let fetchRequest = NSFetchRequest(entityName: "Location")
fetchRequest.returnsObjectsAsFaults = false;
var fetchResults = appDelegate.managedObjectContext!.executeFetchRequest(fetchRequest, error: nil)
return fetchResults! as Array<AnyObject>
}
`