Exception thrown every time when saving Data Object (Swift, iOS 9.2, SDK 3.0.8)

Hello - we’re having issues saving data objects - the app throws an exception (EXC_BAD_ACCESS) - whether it’s following the examples in the documentation from scratch, or adding the SDK to a project already in progress (migration from Parse).

We can register/login users, so part of the SDK is working fine (including secret keys etc), just not saving the data objects, whether using sync or async. We’re using SDK 3.0.8, Swift, iOS 9.2 and Xcode 7.2.1.

The exception thrown can be seen in the attached screenshot. The console also outputs the following message: warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.

The code being used is taken directly from here: https://backendless.com/documentation/data/ios/data_saving_data_objects.htm


    class Contact : NSObject {
        
        var objectId : String?
        var name : String?
        var age: Int?
        var phone : String?
        var title : String?
    }




    func saveNewContact() {
        
        let contact = Contact()
        contact.name = "Jack Daniels"
        contact.age = 147
        contact.phone = "777-777-777"
        contact.title = "Favorites"
        
        let dataStore = backendless.data.of(Contact.ofClass())
        
        
        // save object synchronously
        var error: Fault?
        let result = dataStore.save(contact, fault: &error) as? Contact
        if error == nil {
            print("Contact has been saved: \(result!.objectId)")
        }
        else {
            print("Server reported an error: \(error)")
        }
        
        // save object asynchronously
        dataStore.save(
            contact,
            response: { (result: AnyObject!) -> Void in
                let obj = result as! Contact
                print("Contact has been saved: \(obj.objectId)")
            },
            error: { (fault: Fault!) -> Void in
                print("fServer reported an error: \(fault)")
        })
    }    }

I’ve also tried initialising numeric/boolean values as per a recommendation here: http://support.backendless.com/t/save-class-doesnt-work-with-swift-and-xcode which hasn’t solved the exception crash either.

Sorry to bump, but are there any workarounds to the above? Currently we can’t proceed any further with the migration as everything we try causes the above exception, and may need to look elsewhere due to time constraints.

Hi Nicholas,

I just check the sample from documentation:

class Contact : NSObject {
    
    var objectId : String?
    var name : String?
    var age: Int = 0
    var phone : String?
    var title : String?
}


    func saveNewContact() {
        
        let contact = Contact()
        contact.name = "Jack Daniels"
        contact.age = 147
        contact.phone = "777-777-777"
        contact.title = "Favorites"
        
        let dataStore = backendless.data.of(Contact.ofClass())


        // save object synchronously
        var error: Fault?
        let result = dataStore.save(contact, fault: &error) as? Contact
        if error == nil {
            print("Contact has been saved: \(result!.objectId)")
        }
        else {
            print("Server reported an error: \(error)")
        }
        
        // save object asynchronously
        dataStore.save(
            contact,
            response: { (result: AnyObject!) -> Void in
                let obj = result as! Contact
                print("Contact has been saved: \(obj.objectId)")
            },
            error: { (fault: Fault!) -> Void in
                print("fServer reported an error: \(fault)")
        })
    }

Here is a log:
http://support.backendless.com/public/attachments/15688491addba0b3efda2592d6366b06.png</img>
and result in Backendless app console:
http://support.backendless.com/public/attachments/c0574e65c7d6b474ccd59a3198ed2c40.png</img>

So, I cannot reproduce this issue, the doc sample works fine for me.

Can you create the test project demonstrated this problem, and attach here? We will investigate it, and let you know how it goes.

Regard,
Slava

There is one rule:
The data classes (as Contact) should be implemented OUTSIDE of ViewController or AppDelegate classes, maybe in a separate file(s).

From your code it is unclear, but if Contact class is located inside ViewController class - you should move it outside- and all will be OK.

Regards,
Slava

Hi Slava

Thanks for the quick response. Please see project attached. I have zeroed out the app id and Secret Key, but can confirm these do work for us when using the login examples. I have also taken out the lib file as per the latest SDK, to help keep the file size down (200MB SDK is big!) but the Build Settings should all remain.

Nick

BackendlessTest.zip (30.3kB)

Hi Slava

Moving the Contact Class outside of the ViewController class worked - thanks for the tip. Might be worth a comment in the documentation for future users! Glad we can get started really testing Backendless now.

On an aside - is it possible to reduce the size of the iOS SDK? 200MB is quite large - does everything need to be used/copied into the Xcode project if we’re only using Backendless to handle login/logout and submit/query small text only data objects?

Nick

If you don’t need Backendless MediaService - you can remove the folders: libav-11.4, x264, speex-1.2rc2, MediaLibiOS3x from lib folder and from Build Phases. It dramatically reduces the size of project.

“The data classes (as Contact) should be implemented OUTSIDE of ViewController or AppDelegate classes, maybe in a separate file(s).”

Why isn’t information like this included in the documentation? Would have saved me hours.

An assumption is made that this is just a good practice to keep the class definitions separate. We’ll update the doc to make it clearer.

Regards,
Mark

Stijn, with latest Backendless SDK (3.0.28 release) you may implement data model class inside of ViewController class.