我正在写一本字典应用程序,我试图导入从字符串的原始数据,每字一个字符串。除此之外,原始输入字符串包含相应单词所属的部分的名称。在我的数据模型我有Word
S和PartOfSpeech
一个独立的实体,我要创建的类型PartOfSpeech
的一个实体为每个语音独特的部分有可能是在输入的字符串,并建立从Word
S上的关系,相关言语之语。该PartOfSpeech
实体只有一个属性约,name
,和一对许多关系到Word
: 核心数据,在缓存的NSMutableDictionary NSManagedObjects,问题
我的第一个执行卷入独特PartOfSpeech
实体的可变数组缓存他们,每次用谓词过滤它的。它有效,但速度很慢。我决定通过在一个NSDictionary中缓存PartsOfSpeech来加快速度,现在当我在导入后尝试并保存数据存储时,出现错误“无法使用自己的存储之外的引用保存对象”。它看起来像是在字典中的问题,但我该如何解决它?
这里是工作的代码: (在sniplets managedObjectContext
是伊娃和processStringsInBackground:
方法在后台线程使用performSelectorInBackground:withObject:
方法运行)
- (void) processStringsInBackground:(NSFetchRequest *)wordStringsReq {
NSError *err = NULL;
NSFetchRequest *req = [[NSFetchRequest alloc] init];
[req setEntity:[NSEntityDescription entityForName:@"PartOfSpeech" inManagedObjectContext:managedObjectContext]];
err = NULL;
NSMutableArray *selectedPartsOfSpeech = [[managedObjectContext executeFetchRequest:req error:&err] mutableCopy];
NSPredicate *p = [NSPredicate predicateWithFormat:@"name like[c] $name"];
// NSPredicate *formNamePredicate = [NSPredicate predicateWithFormat:<#(NSString *)predicateFormat#>]
...
for (int i = 0; i < count; i++){
...
currentPos = [self uniqueEntityWithName:@"PartOfSpeech" usingMutableArray:selectedPartsOfSpeech predicate:p andDictionary:[NSDictionary dictionaryWithObject:partOfSpeech forKey:@"name"]];
...
}
}
- (NSManagedObject *) uniqueEntityWithName:(NSString *) entityName usingMutableArray:(NSMutableArray *)objects predicate:(NSPredicate *)aPredicate andDictionary:(NSDictionary *) params {
NSPredicate *p = [aPredicate predicateWithSubstitutionVariables:params];
NSArray *filteredArray = [objects filteredArrayUsingPredicate:p];
if ([filteredArray count] > 0) {
return [filteredArray objectAtIndex:0];
}
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:managedObjectContext];
NSArray *dicKeys = [params allKeys];
for (NSString *key in dicKeys) {
[newObject willChangeValueForKey:key];
[newObject setPrimitiveValue:[params valueForKey:key] forKey:key];
[newObject didChangeValueForKey:key];
}
[objects addObject:newObject];
return newObject;
}
这里是一样的,但高速缓存使用NSMutableDictionary,之后未能保存:
- (void) processStringsInBackground:(NSFetchRequest *)wordStringsReq {
NSError *err = NULL;
[req setEntity:[NSEntityDescription entityForName:@"PartOfSpeech" inManagedObjectContext:managedObjectContext]];
NSArray *selectedPartsOfSpeech = [managedObjectContext executeFetchRequest:req error:&err];
NSMutableDictionary *partsOfSpeechChache = [[NSMutableDictionary alloc] init];
for (PartOfSpeech *pos in selectedPartsOfSpeech) {
[partsOfSpeechChache setObject:pos forKey:pos.name];
}
...
for (int i = 0; i < count; i++){
...
currentPos = [self uniqueEntity:@"PartOfSpeech" withName:partOfSpeech usingDictionary:partsOfSpeechChache];
...
}
}
- (NSManagedObject *)uniqueEntity:(NSString *) entityName withName:(NSString *) name usingDictionary:(NSMutableDictionary *) dic {
NSManagedObject *pos = [dic objectForKey:name];
if (pos != nil) {
return pos;
}
NSManagedObject *newPos = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:managedObjectContext];
[newPos willChangeValueForKey:@"name"];
[newPos setPrimitiveValue:name forKey:@"name"];
[newPos didChangeValueForKey:@"name"];
[dic setObject:newPos forKey:name];
return newPos;
}
你能帮我找到问题吗?
最好的问候, Timofey。
哦,那类的两个变体之间的差异只在于部分我张贴在这里。我也尝试从-processStringsInBackground:方法内的字典中检索PartOfSpeech,但它也不起作用。我认为NSArray和NSDictionary保持引用它们包含的对象的方式可能有一些区别,但是在那里也没有发现任何内容。我也尝试使用NSCache而不是NSMutableDictionary,但再次无济于事。 – Ibolit 2011-03-23 13:28:12