3

I am saving data into Coredata Entity Named Offers.. it Works Fine when i Only Store and fetch data, but when i delete all data from entity and try to save data again it gives following exception.

*** First throw call stack:
(
0   CoreFoundation                      0x000000010989ad85             __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000108ea4deb objc_exception_throw + 48
2   CoreData                            0x0000000108595030 _PFManagedObject_coerceValueForKeyWithDescription + 2864
3   CoreData                            0x000000010856cea1 _sharedIMPL_setvfk_core + 177
4                                0x0000000107f07ace _TFC618FeaturedController13saveOfferDatafCSo12NSDictionaryT_ + 5902
5                                 0x0000000107f0aa4b _TFFC618FeaturedController16loadFeatureOfferFT_T_U_FTSbCSo12NSDictionary_T_ + 1371
6                                0x0000000107e7d97f _TFZFC614ServiceWrapper16getFeaturedOfferFTSS6headerCSo12NSDictionary6paramsS1_17completionHandlerFT7successSb8responseS1__T__T_U_FTSbS1__T_ + 95
7                                 0x0000000107f4608f _TFZFC612Servicelayer17getDataWithObjectFTCSo12NSDictionary7apiNameCSo8NSString17completionHandlerFT7successSb8responseS1__T__T_U_FGVS_8ResponsePs9AnyObject_CSo7NSError_T_ + 2415
8                                0x0000000107f93277 _TTRXFo_oGV68ResponsePs9AnyObject_CSo7NSError__dT__XFo_iGS0_PS1__S2___dT__ + 151
9                                0x0000000107f91b7d _TFFFC67Request8responseuRxS_22ResponseSerializerTyperFT5queueGSqPSo17OS_dispatch_queue__18responseSerializerx17completionHandlerFGVS_8Responsewx16SerializedObjectwx11ErrorObject_T__DS0_U_FT_T_U_FT_T_ + 797
10                                0x0000000107f903c4 _TPA__TFFFC67Request8responseuRxS_22ResponseSerializerTyperFT5queueGSqPSo17OS_dispatch_queue__18responseSerializerx17completionHandlerFGVS_8Responsewx16SerializedObjectwx11ErrorObject_T__DS0_U_FT_T_U_FT_T_ + 164
11                               0x0000000107e4d457 _TTRXFo__dT__XFdCb__dT__ + 39
12  libdispatch.dylib                   0x000000010c4edd9d _dispatch_call_block_and_release + 12
13  libdispatch.dylib                   0x000000010c50e3eb _dispatch_client_callout + 8
14  libdispatch.dylib                   0x000000010c4f61ef _dispatch_main_queue_callback_4CF + 1738
15  CoreFoundation                      0x00000001097f40f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
16  CoreFoundation                      0x00000001097b5b99 __CFRunLoopRun + 2073
17  CoreFoundation                      0x00000001097b50f8 CFRunLoopRunSpecific + 488
18  GraphicsServices                    0x000000010d07cad2 GSEventRunModal + 161
19  UIKit                               0x000000010a930f09 UIApplicationMain + 171
20                               0x0000000107ed18f2 main + 114
21  libdyld.dylib                       0x000000010c54392d start + 1 
)


// this is delete func

 func deleteData(){


    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context = appDelegate.managedObjectContext;
    let fetchRequest = NSFetchRequest()
    fetchRequest.entity = NSEntityDescription.entityForName("Offers", inManagedObjectContext: context)
   // fetchRequest.includesPropertyValues = false
    do {
        if let results = try context.executeFetchRequest(fetchRequest) as? [NSManagedObject] {
            for result in results {
                context.deleteObject(result)
            }

            try context.save()
        }
    } catch {
        print("failed to clear core data")
    }
}


// this is saveData in coreData func

 func saveOfferData(response:NSDictionary){

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    var context: NSManagedObjectContext;
    print("Response: ", response)

    context = appDelegate.managedObjectContext;
    let Example = NSEntityDescription.insertNewObjectForEntityForName("Offers", inManagedObjectContext: context)


     print("offer::", response.objectForKey("area_id")!)

    let offer = Offer();
      offer.setDatafromServer(response)



  //  NSURL *url = [NSURL URLWithString:@"http://www.cimgf.com/images/photo.PNG"];

    let url = NSURL(string: offer.offer_image!)
       // url.URLString(offer.offer_image as! String)
    let data = NSData(contentsOfURL: url!)
    let uiImage = UIImage(data: data!)

   // uiImage = [UIImage imageWithData:data];


    let imageData = UIImagePNGRepresentation(uiImage!)




    //CRASH IS HERE
    Example.setValue(offer.about, forKey: "about")
    Example.setValue(offer.business_address, forKey: "business_address")
    Example.setValue(Int(offer.business_id!), forKey: "business_id")
    Example.setValue(offer.business_name, forKey: "business_name")
    Example.setValue(offer.discountOriginalPrice, forKey: "discountDiscountedPrice")

    Example.setValue(offer.locality, forKey: "locality")
    Example.setValue(offer.discountPercentageOff, forKey: "discountPercentageOff")
    Example.setValue(offer.end_operating_time, forKey: "end_operating_time")
    Example.setValue(offer.expiry_date, forKey: "expiry_date")
    Example.setValue(offer.gallery, forKey: "gallery")
    Example.setValue(offer.number_of_friend, forKey: "number_of_friend")
    Example.setValue(offer.offer_available, forKey: "offer_available")
    Example.setValue(offer.offer_id, forKey: "offer_id")
   // Example.setValue(imageData, forKey: "offer_image")
    Example.setValue(offer.offer_nature, forKey: "offer_nature")
    Example.setValue(offer.offer_nature, forKey: "offer_nature_value")
    Example.setValue(offer.offer_title, forKey: "offer_title")
    Example.setValue(offer.on_going, forKey: "on_going")
    Example.setValue(offer.offerNatureValue, forKey: "offerNatureValue")
    Example.setValue(offer.is_inviteable, forKey: "is_inviteable")
    Example.setValue(offer.sharing_message, forKey: "sharing_message")
    Example.setValue(offer.sharing_subject, forKey: "sharing_subject")
    Example.setValue(offer.sharing_url, forKey: "sharing_url")
    Example.setValue(offer.friend_discount, forKey: "friend_discount")
    Example.setValue(offer.start_operating_time, forKey: "start_operating_time")
    Example.setValue(offer.terms, forKey: "terms")
    Example.setValue(offer.area_id, forKey: "area_id")
    Example.setValue(offer.categories, forKey: "categories")
    Example.setValue(offer.category_id, forKey: "category_id")
    Example.setValue(offer.category_name, forKey: "category_name")
    Example.setValue(offer.featured, forKey: "featured")

    // Sharing Parameters

    do {
        try Example.managedObjectContext?.save()
        print("saved successfully")
    } catch {
        print(error)
    }


 }
sschale
  • 5,168
  • 3
  • 29
  • 36
AyAz
  • 2,027
  • 2
  • 21
  • 28
  • In your catch, do `print(error)` to see what's wrong (if it gets that far). – sschale Jun 01 '16 at 05:16
  • @sschale i got it it is error of image saving within coredata, may this is wrong way that i used... – AyAz Jun 01 '16 at 05:18
  • What line is failing? – sschale Jun 01 '16 at 05:19
  • Also, take a look at some of the solutions here: http://stackoverflow.com/questions/1230858/iphone-core-data-crashing-on-save – sschale Jun 01 '16 at 05:21
  • let url = NSURL(string: offer.offer_image!) // url.URLString(offer.offer_image as! String) let data = NSData(contentsOfURL: url!) let uiImage = UIImage(data: data!) let imageData = UIImagePNGRepresentation(uiImage!) Example.setValue(imageData, forKey: "offer_image") this line execption. that i mention above in question – AyAz Jun 01 '16 at 05:22
  • 1
    One thing to fix first - don't name variables starting with Capitals - it's super confusing. Make them camelCase. (So it should be example.setValue). – sschale Jun 01 '16 at 05:24
  • You cannot delete object while itreating the array. – Muhammad Adil Jun 01 '16 at 05:27
  • @sschale this is valid Point.. but not related to the exception.. – AyAz Jun 01 '16 at 05:33
  • @MuhammadAdil They aren't being deleted in that sense, the context is marking them for deletion, and that's not where his crash his. – sschale Jun 01 '16 at 05:34
  • @MuhammadAdil crash is on savedOfferData and i comments where crash occured it is in image // Example.setValue(imageData, forKey: "offer_image") – AyAz Jun 01 '16 at 05:44

0 Answers0