核心数据删除异常

时间:2011-05-20 02:15:48

标签: core-data

我正在尝试清理状态未完成的对象。然后我从Core Data中删除这些对象时获得了删除异常。你能帮我指出错误吗?感谢

以下是代码:

- (void) cleanDownloadQueue {
NSMutableArray *cleanedAssetFiles = [[NSMutableArray alloc] init];
//get objects from context
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[self AssetFileEntityDescription]];
NSString *name = @"downloadStatus"; 
NSInteger value = COMPLETED;
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"%K != %i",name,value];
[fetchRequest setPredicate:predicate];
NSArray *results = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
//NSLog(@"current length results length: %i", [results count]);
if ([results count] > 0) {
    for (NSObject *obj in results){
        if ([obj isKindOfClass:[AssetFile class]]) {
            AssetFile *assetFile = (AssetFile *) obj;
            //assetFile.srAsset.assetFile = nil;
            [cleanedAssetFiles addObject:assetFile];

        }
    }
}

[fetchRequest release];
[results release];
for (AssetFile *afile in cleanedAssetFiles) {
    [self.managedObjectModel delete:afile];
}
if ([cleanedAssetFiles count] > 0) {
    [self saveManagedObjectChanges];
}
[cleanedAssetFiles release];

}

这是日志:

 <Error>: -[NSManagedObjectModel delete:]: unrecognized selector sent to instance 0x1d8dd0
May 19 21:57:10 unknown DMC to Go[1182] <Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObjectModel delete:]: unrecognized selector sent to instance 0x1d8dd0'
    *** Call stack at first throw:
    (
        0   CoreFoundation                      0x32a9d64f __exceptionPreprocess + 114
        1   libobjc.A.dylib                     0x35475c5d objc_exception_throw + 24
        2   CoreFoundation                      0x32aa11bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
        3   CoreFoundation                      0x32aa0649 ___forwarding___ + 508
        4   CoreFoundation                      0x32a17180 _CF_forwarding_prep_0 + 48
        5   DMC to Go                           0x0000483f -[dmcIpadAppDelegate cleanDownloadQueue] + 934
        6   DMC to Go                           0x0001b617 +[DmcIpadAppDelegate cleanDownloadQueue] + 82
        7   DMC to Go                           0x0000b35d -[SRListViewController viewDidLoad] + 40
        8   UIKit                               0x3262cf0f -[UIViewController view] + 110
        9   UIKit                               0x3263b5ef -[UIViewController contentScrollView] + 22
        10  UIKit                               0x3263b45f -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 30
        11  UIKit                               0x3263b35d -[UINavigationController _layoutViewController:] + 24
        12  UIKit                               0x3263ae35 -[UINavigationController _startTransition:fromViewController:toViewController:] + 380
        13  UIKit                               0x3263ac43 -[UINavigationController _startDeferredTransitionIfNeeded] + 182
        14  UIKit                               0x3263ab87 -[UINavigationController viewWillLayoutSubviews] + 14
        15  UIKit                               0x3263ab23 -[UILayoutContainerView layoutSubviews] + 138
        16  UIKit                               0x325fb5fb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 26
        17  CoreFoundation                      0x32a0af03 -[NSObject(NSObject) performSelector:withObject:] + 22
        18  QuartzCore                          0x35f3bbb5 -[CALayer layoutSublayers] + 120
        19  QuartzCore                          0x35f3b96d CALayerLayoutIfNeeded + 184
        20  QuartzCore                          0x35f3b8a7 -[CALayer layoutIfNeeded] + 78
        21  UIKit                               0x325fb5d7 -[UIView(Hierarchy) layoutIfNeeded] + 22
        22  UIKit                               0x326a3775 -[UIViewController window:willAnimateRotationToInterfaceOrientation:duration:] + 356
        23  UIKit                               0x326a2bc1 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 3100
        24  UIKit                               0x326a6677 -[UIWindow _setRotatableViewOrientation:duration:force:] + 54
        25  UIKit                               0x3263a8eb -[UIWindow _updateToInterfaceOrientation:duration:force:] + 74
        26  UIKit                               0x32639551 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 540
        27  UIKit                               0x326390ad -[UIWindow setDelegate:] + 168
        28  UIKit                               0x32638fa9 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 64
        29  UIKit                               0x32637611 -[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:] + 460
        30  UIKit                               0x326165a7 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 506
        31  UIKit                               0x3261632f -[UIView(Hierarchy) _postMovedFromSuperview:] + 106
        32  UIKit                               0x325fbc1b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 678
        33  UIKit                               0x325fb96b -[UIView(Hierarchy) addSubview:] + 22
        34  UIKit                               0x3262b35f -[UIWindow addRootViewControllerViewIfPossible] + 202
        35  UIKit                               0x3275653f -[UIWindow setRootViewController:] + 166
        36  DMC to Go                           0x00003055 -[dmcIpadAppDelegate application:didFinishLaunchingWithOptions:] + 792
        37  UIKit                               0x3262b821 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 772
        38  UIKit                               0x32625b65 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 272
        39  UIKit                               0x325fa7d7 -[UIApplication handleEvent:withNewEvent:] + 1114
        40  UIKit                               0x325fa215 -[UIApplication sendEvent:] + 44
        41  UIKit                               0x325f9c53 _UIApplicationHandleEvent + 5090
        42  GraphicsServices                    0x358d2e77 PurpleEventCallback + 666
        43  CoreFoundation                      0x32a74a97 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
        44  CoreFoundation                      0x32a7683f __CFRunLoopDoSource1 + 166
        45  CoreFoundation                      0x32a7760d __CFRunLoopRun + 520
        46  CoreFoundation                      0x32a07ec3 CFRunLoopRunSpecific + 230
        47  CoreFoundation                      0x32a07dcb CFRunLoopRunInMode + 58
        48  UIKit                               0x32624d49 -[UIApplication _run] + 372
        49  UIKit                               0x32622807 UIApplicationMain + 670
        50  DMC to Go                           0x00002927 main + 82
        51  DMC to Go                           0x000028d0 start + 40

1 个答案:

答案 0 :(得分:0)

您无法使用managedObjectModel删除对象。

只有managedObjectContext可以删除对象:

for (AssetFile *afile in cleanedAssetFiles) {
    [afile.managedObjectContext delete:afile];
}