我正在開發一個允許用戶在地圖上繪製路線的應用程序。 我有三個實體:核心數據:如何更新NSSet類型的值
用戶應該能夠保存Track
,然後繼續一段時間後繪製它。我的問題是更新 - 據我瞭解,將Track
對象添加到現有的對象,我應該取TrackName
,更新它的Set<Track>
並保存上下文(不知道這個,這是正確的方法?)。我存儲了用戶在自定義類型Line
(它與跟蹤實體具有相同變量名稱)中繪製的點,但我不知道如何用Array<Line>
替換Set<Tracks>
。有沒有什麼辦法可以創建類型爲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)
但航跡點是其他類型。
編輯1.
帕維爾,我試圖做這種方式,但發現了新的TRACKNAME和名稱對象被創建每次,當我取的軌道加入我得到空數組。下面的代碼:
第一,用戶名跟蹤他
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文件記錄
我認爲這是非常就像你寫的一樣。我在這裏做錯了什麼?
採取數據保存時,你是不是對你的''TRACKNAME的name'屬性設置值'object('trackEnt')。因此它不匹配您的獲取請求中的謂詞。 – pbasdf