Unable to add data to nsarray type

data_modelling
#1

Hi, i am trying to update a property of my cblmodel with an array. The array consists of objects which are of a model fetched from coredata.

example:
class Mycblmodel {
// other properties
var propertyReqd : NSArray?
// rest codes for initialisation using class
}

inside controller I am updating the key :

let doc = db.documentWithID(“p:(ID)”)
do{
try doc?.update({ (newData) -> Bool in
newData[“propertyReqd”] = reqdArray

            return true
        })
    } catch let ErrorType {
        print("\n \(ErrorType) : Couldn't update data in database")
    }

reqdArray is a NSArray which holds the objects of a data model fetched from coredata.

While creating/updating i get this error –

‘NSInvalidArgumentException’, reason: ‘- [App.MyCBLModelClass cbjson_encodeTo:canonical:]: unrecognized selector sent to instance 0x7fb15ab0ac70’
*** First throw call stack:
(
0 CoreFoundation 0x000000010256ff45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000101b8ddeb objc_exception_throw + 48
2 CoreFoundation 0x000000010257856d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001024c5eea forwarding + 970
4 CoreFoundation 0x00000001024c5a98 _CF_forwarding_prep_0 + 120
5 App 0x0000000100c9afd1 -[NSArray(CBJSONEncoder) cbjson_encodeTo:canonical:] + 225
6 App 0x0000000100c9b558 -[NSDictionary(CBJSONEncoder) cbjson_encodeTo:canonical:keyFilter:error:] + 584
7 App 0x0000000100c9a957 -[CBJSONEncoder encode:] + 137
8 App 0x0000000100c3d239 +[CBL_Revision asCanonicalJSON:error:] + 125
9 App 0x0000000100cc3bf3 -[CBL_SQLiteStorage addDocID:prevRevID:properties:deleting:allowConflict:validationBlock:status:error:] + 212
10 App 0x0000000100c9572a -[CBLDatabase(Insertion) putDocID:properties:prevRevisionID:allowConflict:status:error:] + 1067
11 App 0x0000000100c65b1d -[CBLDocument putProperties:prevRevID:allowConflict:error:] + 421
12 App 0x0000000100c69836 -[CBLUnsavedRevision save:] + 78
13 App 0x0000000100c65c9a -[CBLDocument update:error:] + 122
14 App 0x0000000100bad35c TFC8App20RootViewController16saveDatafS0_FT_T + 5100
15 App 0x0000000100babad7 TFC8App20RootViewController60updateSplitViewDetailToREquiredFunctionS0_FT_T + 1351
16 App 0x0000000100ba9c3a TFC8App20RootViewController23generateButtonTappedfS0_FPSs9AnyObject_T + 42
17 App 0x0000000100ba9c86 TToFC8App20RootViewController23generateButtonTappedfS0_FPSs9AnyObject_T + 54
18 UIKit 0x0000000103103e91 -[UIApplication sendAction:to:from:forEvent:] + 92
19 UIKit 0x000000010326f4d8 -[UIControl sendAction:to:forEvent:] + 67
20 UIKit 0x000000010326f7a4 -[UIControl _sendActionsForEvents:withEvent:] + 311
21 UIKit 0x000000010326e8d4 -[UIControl touchesEnded:withEvent:] + 601
22 UIKit 0x0000000103171ed1 -[UIWindow _sendTouchesForEvent:] + 835
23 UIKit 0x0000000103172c06 -[UIWindow sendEvent:] + 865
24 UIKit 0x00000001031222fa -[UIApplication sendEvent:] + 263
25 UIKit 0x00000001030fcabf _UIApplicationHandleEventQueue + 6844
26 CoreFoundation 0x000000010249c011 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
27 CoreFoundation 0x0000000102491f3c __CFRunLoopDoSources0 + 556
28 CoreFoundation 0x00000001024913f3 __CFRunLoopRun + 867
29 CoreFoundation 0x0000000102490e08 CFRunLoopRunSpecific + 488
30 GraphicsServices 0x00000001069a9ad2 GSEventRunModal + 161
31 UIKit 0x000000010310230d UIApplicationMain + 171
32 App 0x0000000100ae1a4d main + 109
33 libdyld.dylib 0x0000000104ce492d start + 1
)

#2

See this issue. You need to use JSON-compatible objects in your model.

#3

Have already read that, since it is a url , it can be converted to string. But what about model objects in an array? those objects are fetched from coredata.

#4

Those objects need to be JSON compatible objects, and not other model objects (maybe you can just store the IDs of the model objects in there instead?). You need to either do the conversion yourself before saving it or I think as a hack you might be able to implement the missing method on your model object ;).