【问题标题】:CoreData crash while saving after setting value for NSManagedObject's property为 NSManagedObject 的属性设置值后保存时 CoreData 崩溃
【发布时间】:2011-07-26 08:40:01
【问题描述】:

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

  1. 我正在使用 NSFetchResultsController 使用 CoreData 中的数据填充我的 UITableView
  2. 我懒得下载图片以在单元格中显示它们。
  3. 当图标下载结束时,我将图像数据转换为 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:]

可能的问题:

  1. CoreData 在保存时可以处理大量数据。转换为 base64 的图像可能包含大量字符。
  2. 第 #5、#6、#7 行说明了 NSFetchResultsController。

已解决:

我只是简单地释放了 NSPredicate,而它是自动释放的对象,所以在保存 CoreDatra 后,我的 NSFetchResultsController 想要更新它的数据,但一切都崩溃了。

【问题讨论】:

  • 您最好回答自己的问题,而不是用答案编辑原始问题。然后你就可以接受答案了。

标签: iphone ios core-data nsfetchedresultscontroller


【解决方案1】:

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

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

希望你能得到你的解决方案........

【讨论】:

  • 我已经解决了我的问题,我在主帖中对此进行了描述,但我很感激你的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多