我正在开发一个允许用户在地图上绘制路线的应用程序。 我有三个实体:
用户应该能够保存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文件时,我看到创建了新的Name和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文件中有记录
我认为这和你写的几乎一样。我在这里做错了吗?
答案 0 :(得分:0)
对于第一个,要添加新的Track
,您无需手动将其添加到Set<Track>
。相反,您只需要在上下文中插入新的Track
对象,然后为其设置TrackName
,然后保存上下文,它将完美地工作,它将自动更新TrackName
实体。
是的,您可以为Track
创建一些类别,将其插入上下文并从Line
或trackPoints