2011-09-21 45 views
2

我想執行一個取請求:未保存的管理對象上下文產生異常或ANY

NSFetchRequest *fetchRequest = [NSFetchRequest new]; 
[fetchRequest setIncludesPendingChanges:YES];   
NSSet *set = [NSSet setWithObjects:@"TESTNAME",@"TEST", nil];   
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"NONE name IN %@", set]]; 
執行該應用被終止與例外上的未保存的NSManagedObjectContext請求時

*終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:'ALL或ANY操作符的左側必須是NSArray或NSSet。

如果我保存爲預期的背景下臨刑前的所有作品。關於這個原因的任何想法?

注:這發生在應用程序啓動時綁定數據導入。

回答

0

我遇到同樣的問題。 我有一個由子謂語組成的大謂詞(使用ANY內部),並且在刪除「ANY」子類別後,我沒有發生任何崩潰。

又如何,如果你是通過UIManagedDocument使用自動保存保存上下文?


---- ----編輯

的問題是通過將try/catch語句並重新初始化謂詞根本解決:

+ (NSArray *)productsByColorName:(NSString *)name { 
    NSManagedObjectContext *context = [DatabaseController sharedInstance].database.managedObjectContext; 
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:[[self class] tableName]]; 
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K =[cd] %@", @"color.name", name]; 
    request.predicate = predicate; 

    NSError *error = nil; 
    NSArray *matches = nil; 

    @try { 
     matches = [context executeFetchRequest:request error:&error]; 

    } @catch (NSException *ex) { 
     NSLog(@"Exception: %@", [ex reason]); 

     predicate = [NSPredicate predicateWithFormat:@"%K =[cd] %@", @"color.name", name]; 
     request.predicate = predicate; 

     matches = [context executeFetchRequest:request error:&error]; 
    } 

    return matches; 
} 

好像裏面有一個問題,如果上下文在更改後未保存,則數據庫工作方式不同。

也許我做錯了(通過變通方法將其固定),但是這種方法固定我崩潰。希望它能幫助別人。

相關問題