0

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>
}

`

user3050491
  • 95
  • 4
  • 12
  • Are you sure whatever function you use to delete the entities is working? Since the fetch request specifies all entity objects and the data is persistent the faults could be locations from previous runs of the program. – Pat Wallace Jan 24 '15 at 04:22
  • I think you may be right about that. Seems as though my delete function wasn't deleting all of the instances of entities. – user3050491 Jan 24 '15 at 05:24

0 Answers0