创建核心数据实体工作但获取实体不起作用

时间:2013-10-31 15:14:09

标签: ios objective-c core-data magicalrecord

所以,我有一个`NSManagedObject的用户,边界和预设。预设始终与单个用户相关联。用户可以有许多预设。每个Preset可以绑定一个边界。

基本上,用户有一个预设,他可以保存到他拥有的每个边界。

用户与Preset有多对多的关系。 边界与Preset有多对多的关系。

我正在尝试生成用户的预设列表减去已经绑定到边界的预设。

我正在使用Magical Records。

我的问题是当我创建一个新的用户和边界时,这有效:

Boundary *boundary = [Boundary MR_createEntity];
boundary.name = @"test boundary";

UserDB *user = [UserDB MR_createEntity];
user.username = @"test User";

Preset *preset01 = [Preset MR_findFirstByAttribute:@"nameDisplay" withValue:@"C4"];
DLog(@"preset01.nameDisplay: %@", preset01.nameDisplay);
Preset *preset02 = [Preset MR_findFirstByAttribute:@"nameDisplay" withValue:@"B"];
DLog(@"preset02.nameDisplay: %@", preset02.nameDisplay);

[boundary setPresets:[NSSet setWithObject:preset01]];
[user setPresets:[NSSet setWithObjects:preset01, preset02, nil]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@ AND boundary != %@", user, boundary];

NSArray *presetsList = [Preset MR_findAllWithPredicate:predicate];

DLog(@"presetsList: %@", presetsList);

所以我将preset01设置为边界。我将preset01和preset02设置为用户。所以我需要一个向用户显示preset02的列表(因为preset01已经绑定到边界,用户不能再添加它)。

DEBUG | -[LoginViewController viewDidLoad] | preset01.nameDisplay: C4
DEBUG | -[LoginViewController viewDidLoad] | preset02.nameDisplay: B
DEBUG | -[LoginViewController viewDidLoad] | presetList: (
    "<SoilTestPointPreset: 0x1e06b8b0> (entity: Preset; id: 0x1e06bca0 <x-coredata://7476DB86-AF79-445C-B3AE-6C91088704A0/Preset/p98> ; data: {\n    attributes = \"<relationship fault: 0x1e074000 'attributes'>\";\n    boundary = nil;\n    gpsLocation = nil;\n    nameDisplay = B;\n    nameTitle = nil;\n    rgbColor = \"0x1e06b1d0 <x-coredata://7476DB86-AF79-445C-B3AE-6C91088704A0/RGBColor/p49>\";\n    testing = nil;\n    user = \"0x1e05ff60 <x-coredata:///UserDB/t59757D99-2FBF-4FBF-97AF-39582FC4B5503>\";\n})"

这就是我的期望。但现在当我获取User和Boundary对象时:

Boundary *boundary = [Boundary MR_findFirstByAttribute:@"boundaryID" withValue:@3748];
DLog(@"boundary.name: %@", boundary.name);

UserDB *user = [UserDB MR_findFirstByAttribute:@"uid" withValue:@99];
DLog(@"user.username: %@", user.username);

我的数组是空的:

DEBUG | -[LoginViewController viewDidLoad] | boundary.name: 997677
DEBUG | -[LoginViewController viewDidLoad] | user.username: thatPerson
DEBUG | -[LoginViewController viewDidLoad] | preset01.nameDisplay: C4
DEBUG | -[LoginViewController viewDidLoad] | preset02.nameDisplay: B
DEBUG | -[LoginViewController viewDidLoad] | presetList: (
)

为什么从核心数据中获取用户和边界会改变结果而不是创建它们?

更新:

我补充说:

Boundary *boundary = [Boundary MR_findFirstByAttribute:@"boundaryID" withValue:@3748];
DLog(@"boundary.name: %@", boundary.name);
DLog(@"boundary.presets.count: %d", boundary.presets.count); // Added

UserDB *user = [UserDB MR_findFirstByAttribute:AVI_UID withValue:@99];
DLog(@"user.username: %@", user.username);
DLog(@"user.presets.count: %d", user.presets.count); // Added

DLog(@"AFTER | boundary.presets.count: %d", boundary.presets.count); //Added
DLog(@"AFTER | user.presets.count: %d", user.presets.count); //Added

[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait]; //Added

他们的用户和边界有预设关系:

DEBUG | -[LoginViewController viewDidLoad] | boundary.name: 997677
DEBUG | -[LoginViewController viewDidLoad] | BEFORE | boundary.presets.count: 0
DEBUG | -[LoginViewController viewDidLoad] | user.username: iDealer
DEBUG | -[LoginViewController viewDidLoad] | BEFORE | user.presets.count: 0
DEBUG | -[LoginViewController viewDidLoad] | preset01.nameDisplay: C4
DEBUG | -[LoginViewController viewDidLoad] | preset02.nameDisplay: B
DEBUG | -[LoginViewController viewDidLoad] | AFTER | boundary.presets.count: 1
DEBUG | -[LoginViewController viewDidLoad] | AFTER | user.presets.count: 2
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x20831a80) → Saving <NSManagedObjectContext (0x20831a80): *** DEFAULT ***> on *** MAIN THREAD ***
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x20831a80) → Save Parents? 1
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x20831a80) → Save Synchronously? 1
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1f59e860) → Saving <NSManagedObjectContext (0x1f59e860): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1f59e860) → Save Parents? 0
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1f59e860) → Save Synchronously? 1
__70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke21(0x1f59e860) → Finished saving: <NSManagedObjectContext (0x1f59e860): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
DEBUG | -[LoginViewController viewDidLoad] | presetList: (
)

2 个答案:

答案 0 :(得分:0)

您需要先保存数据,然后才能从商店获取数据。使用MR_saveToPersistentStoreAndWait或其他变体。提取始终是'磁盘',所以首先保存应该解决这个问题。

答案 1 :(得分:0)

在一堆游戏之后,我将问题缩小到我正在使用的NSPredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@ AND boundary != %@", user, boundary];

查询中似乎存在NOTNONE的问题。得到了我发布的另一个问题的答案:NSPredicate with a !=?

答案涉及使用SUBQUERY代替NOT

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@ AND SUBQUERY(boundary, $p, $p == %@).@count == 0", user, boundary];

我猜我正在制作的提取物很好,因为不好NSPredicate会返回0,但我没有肯定。