我正在开发一个允许用户在地图上绘制路线的应用程序。
我有三个实体:

ios - 核心数据:如何更新NSSet类型的值-LMLPHP

用户应该能够保存Track,然后过一会儿继续绘制它。我的问题是更新-据我了解,要将Track对象添加到现有对象中,我应该获取TrackName,更新它的Set<Track>并保存上下文(不确定这一点,这是正确的方法吗?)。我将用户绘制的点存储在自定义类型Line的数组中(它与Track实体具有相同的变量名),但是我不知道如何用Set<Tracks>替换Array<Line>。有什么方法可以创建类型为Track的数组?还是可以为Track添加某种构造函数?

现在我正在这样做:

var request = NSFetchRequest(entityName: "TrackName")
        var pred = NSPredicate(format: "name == %@ and username.userName == %@", trackName!, currentUser.login)
        request.predicate = pred
        request.returnsObjectsAsFaults = false

        var result : NSArray = NSArray()

        do
        {
             result = try context.executeRequest(request) as! [TrackName]
        }
        catch{}

        var currTrack = result[0] as! TrackName


并尝试做这样的事情

currTrack.track = Set<Track>(arrayLiteral: trackPoints)


但是trackPoints是其他类型。

编辑1。

帕维尔(Pavel),我尝试这样做,但是发现每次都创建新的TrackName和Name对象,当我获取添加的轨道时,我得到了空数组。下面的代码:

首先,用户命名他的足迹

func logName(name:String)
    {
        var appDel : AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate);
        var context : NSManagedObjectContext = appDel.managedObjectContext
        var nameEntity = NSEntityDescription.entityForName("Name", inManagedObjectContext: context)
        var forName = Name(entity: nameEntity!, insertIntoManagedObjectContext: context)
        forName.userName = currentUser.login

        var trackNameEntity = NSEntityDescription.entityForName("TrackName", inManagedObjectContext: context)
        var newTrack = TrackName(entity: trackNameEntity!, insertIntoManagedObjectContext: context)

        newTrack.name = name
        newTrack.username = forName
        do{
            try context.save()
        }
        catch{}
        trackName = name
    }


保存轨道:

func saveTrack()
    {
        var appDel : AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate);
        var context : NSManagedObjectContext = appDel.managedObjectContext

        var nameDesc = NSEntityDescription.entityForName("Name", inManagedObjectContext: context)
        var nameEnt = Name(entity: nameDesc!, insertIntoManagedObjectContext: context)
        nameEnt.userName = currentUser.login

        var trackNameDesc = NSEntityDescription.entityForName("TrackName", inManagedObjectContext: context)
        var trackEnt = TrackName(entity: trackNameDesc!, insertIntoManagedObjectContext: context)
        trackEnt.username = nameEnt
                 var trackDesc = NSEntityDescription.entityForName("Track", inManagedObjectContext: context)
        for p in trackPoints
        {
            var newTrack = Track(entity: trackDesc!, insertIntoManagedObjectContext: context)
            newTrack.trackname = trackEnt
            newTrack.startLat = p.startLat
            newTrack.startLon = p.startLon
            newTrack.endLat = p.endLat
            newTrack.endLon = p.endLon

        do {try context.save()}

        catch{}
    }
    }


当我这样保存并检查.sqlite文件时,我看到创建了新的名称和TrackName。

正在取得:

func fetchWithPredicate(table: String, pred: NSPredicate)->NSArray
    {
        var appDel : AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate);
        var context : NSManagedObjectContext = appDel.managedObjectContext
        var request = NSFetchRequest(entityName: table)
        request.predicate = pred//NSPredicate(format: pred)
        request.returnsObjectsAsFaults = false
        var results: NSArray = NSArray()
        do{
            results = try context.executeFetchRequest(request)
        }

        catch{}

        return results
    }


从这里调用fetchWithPredicate:

func getTrack(trackName: String)->[Track]
    {
        let pred = NSPredicate(format: "trackname.name == %@", trackName)
        return fetchWithPredicate("Track", pred: pred) as! [Track]
    }


它返回一个空数组,但是.sqlite文件中有记录

我认为它与您写的几乎相同。我在这里做错什么了吗?

最佳答案

对于第一个,要添加新的Track,您无需手动将其添加到Set<Track>。取而代之的是,您只需要在上下文中插入新的Track对象,然后为其设置TrackName,然后保存上下文,它就可以完美地工作,它将自动更新TrackName实体。

是的,您可以为Track创建一些类别,该类别将在上下文中插入它并从LinetrackPoints中获取数据

10-08 03:48