iPhone - 在核心数据中创建非持久性实体

时间:2010-04-22 19:22:16

标签: cocoa-touch cocoa core-data

我想使用实体对象但不存储它们......我读到我可以像这样创建它们:

myElement = (Element *)[NSEntityDescription insertNewObjectForEntityForName:@"Element" inManagedObjectContext:managedObjectContext];

然后删除它们:

[managedObjectContext deleteObject:myElement];

然后我可以使用我的元素:

myElement.property1 = @"Hello";

虽然我认为这可能不是最佳的做法,但效果还不错......

然后我尝试在我的UITableView中使用它......问题是初始化后对象被释放了。移动它时我的桌子变空了!

由于

编辑:我也尝试复制元素([myElement copy]),但是我收到错误...

5 个答案:

答案 0 :(得分:0)

你不能这么做吗

Element *myElement = [[Element alloc] init];

然后用它做任何你想做的事情,大概你会把它添加到一个数组中,所以它会说明你的UITableView。

答案 1 :(得分:0)

也许你可以尝试在你的项目中拥有两个商店协调员。一个有持久性,另一个没有持久性。

答案 2 :(得分:0)

考虑使用临时对象---由托管对象上下文像任何其他对象一样处理,但在保存操作时不写入磁盘。它们通常用于模拟仅运行时对象,因为我怀疑你正在尝试这样做。

以下是关于它们的一些信息: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html

http://2pi.dk/tech/cocoa/transient_properties.html

答案 3 :(得分:0)

an answer to a similar question中提取的一个选项是使用nil上下文初始化NSManagedObject:

Element *myElement = [[Element alloc] initWithEntity:entity
                      insertIntoManagedObjectContext:nil];

Element *myElement = [NSEntityDescription insertNewObjectForEntityForName:@"Element"
                                                   inManagedObjectContext:nil];

答案 4 :(得分:0)

我所做的是使用内存商店。您可以按照此处的说明执行此操作:http://commandshift.co.uk/blog/2013/06/06/multiple-persistent-stores-in-core-data/

相关问题