Core Data: How to update value of type NSSet
<p>I'm developing an application that allows user to draw routes on map.
I have three entities:</p>

<p><a href="https://i.stack.imgur.com/IlSxX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IlSxX.png" alt="My entities"></a></p>

<p>User should be able to save <code>Track</code>, and then continue drawing it after a while. My problem is in updating - as i understood, to add <code>Track</code> objects to existing ones, i should fetch <code>TrackName</code>, update it's <code>Set&lt;Track&gt;</code> and save context(not sure about this one, is this right approach?). I store points that user draws in an array of custom type <code>Line</code>(it has same variable names as Track entity), but i don't know how to replace <code>Set&lt;Tracks&gt;</code> with <code>Array&lt;Line&gt;</code>. Is there any way to create array of type <code>Track</code>? Or can i add some kind of constructor for <code>Track</code>?</p>

<p>Right now i'm doing this:</p>

<pre><code>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
</code></pre>

<p>and trying to do something like this</p>

<pre><code>currTrack.track = Set&lt;Track&gt;(arrayLiteral: trackPoints)
</code></pre>

<p>but trackPoints is other type.</p>

<p>Edit 1.</p>

<p>Pavel, i tried to do it this way, but found out that new TrackName and Name objects were created each time, and when i fetch tracks added i get empty array. code below:</p>

<p>first, user names his track</p>

<pre><code>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
    }
</code></pre>

<p>saving track:</p>

<pre><code>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{}
    }
    }
</code></pre>

<p>When i save like this and check .sqlite file, i see that new Name and TrackName are created.</p>

<p>fetching:</p>

<pre><code>func fetchWithPredicate(table: String, pred: NSPredicate)-&gt;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
    }
</code></pre>

<p>fetchWithPredicate is called from here:</p>

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

<p>it returns an empty array, but there are records in the .sqlite file</p>

<p>I think it's pretty much the same as you wrote. Am i doing something wrong here?</p>