我正在寫一本字典應用程序,我試圖導入從字符串的原始數據,每字一個字符串。除此之外,原始輸入字符串包含相應單詞所屬的部分的名稱。在我的數據模型我有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