gpt4 book ai didi

ios - 在 MagicalRecord 中处理 saveWithBlock 和关系时出错

转载 作者:行者123 更新时间:2023-11-29 01:53:12 25 4
gpt4 key购买 nike

我正在使用 MagicalRecord 将我的自定义 CoreData 函数更改为更好的东西。

我有两个实体:报价(视频游戏报价)和系统(游戏机、PC、etx)。一个报价可以有一个或多个系统,我从 Parse 中获取所有数据查询,我保存所有数据的地方。

我只有 8 个系统,所以当我的应用程序启动时,我会获取所有这些系统并使用 Magical Record 保存。然后我调用我的方法来获取一些报价并将 PFObjects 从 Parse 转换为实体。

是这样的

+ (void)createOrUpdateOffersWithArray:(NSArray *)objects completion:(void (^)())completion
{
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {

for (PFObject *object in objects) {

Offer *offer = [Offer MR_findFirstByAttribute:@"offerId" withValue:object.objectId inContext:localContext];

if (offer == nil) {
offer = [Offer MR_createEntityInContext:localContext];
}

// Here I set all the offer values (title, date, etc)...


NSSet *offerSavedSystems = [offer mutableSetValueForKey:@"systems"];

if (offerSavedSystems == nil) {
offerSavedSystems = [[NSMutableSet alloc] init];
}

/* This is a set of all the systems related with the offer */
NSArray *offerSystems = [object objectForKey:@"sistemas"];

NSMutableSet *updatedSystems = [[NSMutableSet alloc] init];

/* Here I query one of my System entities for each in the "offerSystems" array */
for (NSString *systemKey in offerSystems) {
System *system = [System MR_findFirstByAttribute:@"key" withValue:systemKey inContext:localContext];
[updatedSystems addObject:system];
}

offer.systems = updatedSystems;

}

} completion:^(BOOL contextDidSave, NSError *error) {

/* UI update*/

}];

}

奇怪的是,这发生在最后一个 for 循环中。尽管我确定所有 8 个系统都在我的 CoreData 模型中,但这一行

System *system = [System MR_findFirstByAttribute:@"key" withValue:systemKey inContext:localContext]; 

返回零

但最奇怪的是,在 for 循环之前使用 NSLOG,就像这样

  NSLog(@"SYSTEMS %d", [System MR_countOfEntities]);
NSLog(@"SYSTEMS WITH LOCAL CONTEXT %d", [System MR_countOfEntitiesWithContext:localContext]);

我明白了

  SYSTEMS 8
SYSTEMS WITH LOCAL CONTEXT 0

我的系统以前是用这种方法保存的

+ (void)initializeSystems
{
NSArray *systemsKeys = [[self systems] allKeys];

for (NSString *systemKey in systemsKeys) {

System *system = [System MR_findFirstByAttribute:@"key" withValue:systemKey];

if (system == nil) {
system = [System MR_createEntity];
}

[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
system.key = systemKey;
system.name = [self literalForSystem:systemKey];
system.subscribed = [NSNumber numberWithBool:NO];
}];
}
}

我做错了什么?

谢谢你的时间

最佳答案

这行代码可能存在几个问题

System *system = [System MR_findFirstByAttribute:@"key" withValue:systemKey inContext:localContext];
  1. systemKey 值并不存在于您的所有实体中。
  2. 未设置 system.key 值(或 nil)

因此,首先检查 - 获取所有系统实体并记录“键”值。确保您的 key 确实存在。

其次,最好重构你的后台保存代码

+ (void)initializeSystemsWithCompletion:(void (^)())completion
{

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {

NSArray *systemsKeys = [[self systems] allKeys];

for (NSString *systemKey in systemsKeys) {

System *system = [System MR_findFirstByAttribute:@"key" withValue:systemKey inContext:localContext];

if (system == nil) {
system = [System MR_createEntityInContext:localContext];
}

system.key = systemKey;
system.name = [self literalForSystem:systemKey];
system.subscribed = [NSNumber numberWithBool:NO];
}


} completion:^(BOOL success, NSError *error) {

}];

}

关于ios - 在 MagicalRecord 中处理 saveWithBlock 和关系时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31189720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com