我在保存CoreData时遇到问题。要记住的三件事:


我正在使用NSFetchResultsController用CoreData中的数据填充UITableView
我懒于下载图像以在单元格中显示它们。
当图标下载结束时,我将图像数据转换为base64字符串,并将该字符串保存在CoreData中。


问题是,当我在删除图标后尝试保存CoreData更改时,我崩溃了。当我注释了负责对NSManagedObject进行更改的代码的一部分时,一切正常,但是我想知道如何保存这些对象而不会崩溃。

下载图标后,我在这里更新NSManagedObject:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (self.activeDownload && [self.activeDownload length]>0) {
        NSString* base64 = [self.activeDownload base64Encoding]; // converting data to base64
        NSError* error= nil;
        Offer* offerDB2 = [self.offer offerObject]; // Offer is NSManagedObject subclass of my Entity
        if (offerDB2) {
            [offerDB2 setImageData:base64]; // Setting property value (if commented -> no crash)
            [CoreDataHandler save]; // This is where App crashes
        }
    }
    self.activeDownload = nil;
    self.imageConnection = nil;
    [IconDownloader doNext];
}


现在提供课程

#import <CoreData/CoreData.h>

@interface Offer :  NSManagedObject
{
}

@property (nonatomic, retain) NSString * bigInfo;
@property (nonatomic, retain) NSNumber * categoryID;
@property (nonatomic, retain) NSNumber * databaseID;
@property (nonatomic, retain) NSNumber * deleted;
@property (nonatomic, retain) NSString * descriptionString;
@property (nonatomic, retain) NSNumber * endDate;
@property (nonatomic, retain) NSString * extendedInfo;
@property (nonatomic, retain) NSNumber * offerType;
@property (nonatomic, retain) NSString * photoLink;
@property (nonatomic, retain) NSString * physicalUnit;
@property (nonatomic, retain) NSString * shopName;
@property (nonatomic, retain) NSNumber * size;
@property (nonatomic, retain) NSString * sizeUnit;
@property (nonatomic, retain) NSString * smallInfo;
@property (nonatomic, retain) NSNumber * startDate;
@property (nonatomic, retain) NSNumber * unitPrice;
@property (nonatomic, retain) NSString * unitPriceName;
@property (nonatomic, retain) NSString * imageData;

@end

#import "Offer.h"

@implementation Offer

@dynamic bigInfo;
@dynamic categoryID;
@dynamic databaseID;
@dynamic deleted;
@dynamic descriptionString;
@dynamic endDate;
@dynamic extendedInfo;
@dynamic offerType;
@dynamic photoLink;
@dynamic physicalUnit;
@dynamic shopName;
@dynamic size;
@dynamic sizeUnit;
@dynamic smallInfo;
@dynamic startDate;
@dynamic unitPrice;
@dynamic unitPriceName, imageData;

-(NSNumber*)sectionNumber{
    return self.offerType;
}

@end


CrashLog看起来像这样:

*** -[NSCFType evaluateWithObject:]: unrecognized selector sent to instance 0x4354210
Program received signal:  “EXC_BAD_ACCESS”.

#0  0x32668ec0 in objc_msgSend
#1  0x32de2b5e in __CFExceptionProem
#2  0x32de2bcc in -[NSObject doesNotRecognizeSelector:]
#3  0x32d67b18 in ___forwarding___
#4  0x32d5e840 in __forwarding_prep_0___
#5  0x30537cbe in -[NSFetchedResultsController(PrivateMethods) _objectInResults:]
#6  0x30538d16 in -[NSFetchedResultsController(PrivateMethods) _preprocessUpdatedObjects:insertsInfo:deletesInfo:updatesInfo:sectionsWithDeletes:newSectionNames:treatAsRefreshes:]
#7  0x3053aa2c in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]
#8  0x33f765d8 in _nsnote_callback
#9  0x32d9e510 in _CFXNotificationPostNotification
#10 0x33f741b2 in -[NSNotificationCenter postNotificationName:object:userInfo:]
#11 0x304b4388 in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:]
#12 0x3050768a in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:]
#13 0x3049c2b0 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:]
#14 0x304cb186 in -[NSManagedObjectContext save:]


可能的问题:


保存时,CoreData可以处理大量数据。那些转换为base64的图像可以包含很多字符。
第5,#6,#7行说明了NSFetchResultsController。


解决了:

我只是发布了NSPredicate wchich是自动发布的对象,所以保存CoreDatra之后,我的NSFetchResultsController想更新其数据,崩溃了。

最佳答案

您已经清除了如何创建Bean(NowOffer)对象的方式,但我认为存在如下所示的错误创建

NowOffer objBean =(NowOffer *)[NSEntityDescription
                                         insertNewObjectForEntityForName:@“ NowOffer”
                                         inManagedObjectContext:appDelegate.managedObjectContext];

希望你能得到你的Solutin ........

10-07 19:56
查看更多