开发者

CoreData crash while saving after setting value for NSManagedObject's property

开发者 https://www.devze.com 2023-03-23 02:31 出处:网络
I have a problem with saving CoreData. Main 3 things to remember: I\'m using NSFetchResultsController to populate my UITableView with data from CoreData

I have a problem with saving CoreData. Main 3 things to remember:

  1. I'm using NSFetchResultsController to populate my UITableView with data from CoreData
  2. I'm lazy downloading images to show them in cells.
  3. When downloading of icon ends I converta image data to base64 string and save that string in CoreData.

Problem is that when I try to save CoreData changes after receving icons I get crash. When I commented part of code re开发者_C百科sponsible for making changes to NSManagedObject everything worked fine, but I want to know how can I save that objects without crash.

This is where I update NSManagedObject after downloading icon:

- (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];
}

Now Offer class

#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 looks like that:

*** -[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:]

Possible problems:

  1. CoreData while saving can deal with lots of data. Those images converted to base64 can contain lots of characters.
  2. Lines #5,#6,#7 say something about NSFetchResultsController.

SOLVED:

I simply released NSPredicate wchich was autoreleased object, so after saveing CoreDatra my NSFetchResultsController wanted to update its data, everuthing crashed.


You Havent Clear How You Have Create The Object Of Your Bean(NowOffer) Ithink There Is Mistake Create like Given Below

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

Hope You Will Get Your Solutin........

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号