在为NSManagedObject的属性设置值后保存时CoreData崩溃

时间:2011-07-26 08:40:01

标签: iphone ios core-data nsfetchedresultscontroller

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

  1. 我正在使用NSFetchResultsController使用CoreData中的数据填充我的UITableView
  2. 我懒得下载图片以便在单元格中显示它们。
  3. 当下载图标结束时,我将图像数据转换为base64字符串并将该字符串保存在CoreData中。
  4. 问题是,当我尝试在收到图标后保存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:]
    

    可能出现的问题:

    1. CoreData在保存时可以处理大量数据。转换为base64的图像可以包含许多字符。
    2. 第5行,第6行,第7行说一些关于NSFetchResultsController。
    3. 解决:

      我只是发布了NSPredicate wchich是自动释放的对象,所以在保存CoreDatra之后我的NSFetchResultsController想要更新它的数据,永远崩溃了。

1 个答案:

答案 0 :(得分:-1)

你没有清楚如何创建你的Bean的对象(NowOffer)我认为有错误创建如下所示

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

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