在CoreData中保存Swift CLLocation [英] Saving Swift CLLocation in CoreData
问题描述
我试图在Core Data中保存 CLLocation
数据。我的属性设置为一个Transformable对象。
I am trying to save CLLocation
data in Core Data. My property is set as a Transformable object.
有些示例代码可以保存位置数据使用objc转换为 NSValue
。
Some sample code out there indicates one can save the location data using this objc converted to an NSValue
.
CLLocationCoordinate2D myLocation;
NSValue *locationValue = [NSValue valueWithBytes:&myLocation objCType:@encode(CLLocationCoordinate2D)]
// then put locationValue into storage - maybe an ObjC collection class or core data etc.
从数据存储检索对象应该使用此范例工作。
Retrieving the object from the data store is supposed to work using this paradigm.
CLLocationCoordinate2D myLocation;
NSValue *locationValue = // restored from your database or retrieved from your collection class
[locationValue getValue:&myLocation];
我试图推断这个想法并捕获整个 CLLocation
对象转换为存储在
CoreData
中的 NSValue
,目标是恢复 CLLocation
对象。
I am attempting to extrapolate on this idea and capture the entire CLLocation
object into an NSValue
stored in CoreData
, with the goal to recover the CLLocation
object from the store at a later time.
在测试各种方法时,我发现当从Core Data Store解开一个 CLLocation
对象时,返回nil。我使用以下内容将 CLLocation
对象放入商店:
While testing various methods I have found nil returned when trying to unwrap a CLLocation
object from a Core Data Store. I am placing a CLLocation
object into the store using the following:
//verified that I have a good CLLocation here
newManagedObject.setValue(location as CLLocation, forKey: "location")
稍后我尝试打开 CLLocation
let location = detail.valueForKey("location")! as CLLocation
但我发现对象不能正确解开。
But I find that the object does not unwrap properly.
展开的对象如下:
(CLLocation) location = 0x00007fff59537c90 {
ObjectiveC.NSObject = {}
}
所以使用 CLLocation
此时返回 found nil ...错误
我缺少关于存储/检索的明显内容 CLLocation
Am I missing something obvious about storing/retrieving CLLocation
objects using Swift?
UPDATE
丹尼尔的答案帮助我意识到NSValueTransformer不会为我工作,所以我回到我以前使用的方法。基本上使用NSKeyedArchiver编码对象图。我的目标,一如既往,是使用最简单的可能的解决方案,所以我选择不使用托管对象子类,因为我需要的属性已经在现有的CLLocation对象。我将可变字段更改为二进制数据,选中选项(存储在外部记录文件中)。我开始测试这个过程,对主细节模板的最小更改。
Daniel's answer helped me realize that NSValueTransformer was not going to work for me so I went back to an approach I've used before. Basically encode the object graph using NSKeyedArchiver. My goal, as always, is to use the simplest possible solution so I chose not to use managed object subclasses since the properties I need are already in the existing CLLocation object. I changed the transformable field to Binary Data with the option checked (Store in External Record File). I set out to test this process with minimal changes to the master detail template.
//assuming location is a valid CLLocation and data model has a binary data field
let archivedLocation = NSKeyedArchiver.archivedDataWithRootObject(location)
newManagedObject.setValue(archivedLocation, forKey: "location")
从数据存储中恢复对象图
在主vc作为默认准备segue的一部分,通过fetch控制器恢复数据。
Recovering the object graph from the data store
On the main vc as part of the default prepare for segue the data is recovered via the fetch controller.
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let controller = (segue.destinationViewController as UINavigationController).topViewController as DetailViewController
controller.detailItem = object
在细节vc我使用NSKeyedUnarchiver打开我的对象图
On the detail vc I unwrap my object graph using NSKeyedUnarchiver
var lc = NSKeyedUnarchiver.unarchiveObjectWithData(detail.valueForKey!("location") as NSData) as CLLocation
推荐答案
你缺少的是NSValue是用于存储类型,即连续的内存块,而不是对象。 CLLocation是一个对象。
What you are missing is that NSValue is for storing types, i.e., contiguous blocks of memory, not objects. CLLocation is an object.
为了将CLLocation存储在核心数据中,您必须存储每个属性。另一种方法是使用编码器。
In order to store a CLLocation in core data, you will have to store each of the properties. An alternative is to use a Coder.
这篇关于在CoreData中保存Swift CLLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!