在这种情况下,如何从Core Data检索特定数据?

时间:2017-04-11 15:00:25

标签: ios swift core-data

我正在尝试根据特定的地图名称检索坐标(在这种情况下" farm")但不知怎的,我无法让它工作。

我的实体是"地图"和"坐标"具有属性"名称","纬度"和经度"分别。我在哪里犯了错误或者我应该做些什么才能访问仅针对一个地图名称的坐标?

fileprivate var fetchedResultsControllerForCoordinateEntity: NSFetchedResultsController<Coordinate> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Coordinate> = Coordinate.fetchRequest()

fetchRequest.sortDescriptors = [NSSortDescriptor(key: "latitude", ascending: true)]
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "longitude", ascending: true)]

// Create Fetched Results Controller
let fetchedResultsControllerForCoordinateEntity = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)

return fetchedResultsControllerForCoordinateEntity
}()


fileprivate var fetchedResultsControllerForMapEntity: NSFetchedResultsController<Map> = {
// Create Fetch Request
let fetchRequest: NSFetchRequest<Map> = Map.fetchRequest()

// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
fetchRequest.predicate = NSPredicate(format:"name == %@", "farm")

// Create Fetched Results Controller
let fetchedResultsControllerForMapEntity = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)

return fetchedResultsControllerForMapEntity
}()




   var newMap = NSEntityDescription.insertNewObject(forEntityName: "Map", into: managedObjectContext)
    newMap.setValue(mapName, forKey: "name")


    for member in self.markersArray {
        var theCoordinate = NSEntityDescription.insertNewObject(forEntityName: "Coordinate", into: managedObjectContext)
        theCoordinate.setValue(member.latitude, forKey: "latitude")
        theCoordinate.setValue(member.longitude, forKey: "longitude")
        theCoordinate.setValue(newMap, forKey: "newRelationship")
  //newRelationship is a one-to-many relationship, as each map has several coordinates
    }

0 个答案:

没有答案