Exception when saving value of detached CBLModel

15 views
Skip to first unread message

Robbie Bubble

unread,
Sep 21, 2014, 7:59:30 AM9/21/14
to mobile-c...@googlegroups.com
hi all,
i would like to create my CBLModels in the view layer and persist them later in the database.
Referring to the CBLModel.h documentation the init initializer seems to was intended for that:
/** Creates a new "untitled" model object with no document or database at all yet.
    Setting its .database property will cause it to create a CBLDocument.
    (This method is mostly here so that NSController objects can create CBLModels.) */

- (instancetype) init;

When i'm instantiating and setting a value of a detached model the following exception is thrown:

MYModel setValue:ofProperty:], /Users/jenkins/jenkins/workspace/build_cblite_ios_100/couchbase-lite-ios/Source/API/CBLModel.m:447<unknown>:0: error: -[MYView viewDidLoad] : failed: caught "NSInternalInconsistencyException", "Invalid parameter not satisfying: _document"



MYView.swift
public class MYView : UIView {

  override func viewDidLoad() {
     
//Init manager/db
     
var manager : CBLManager = CBLManager.sharedInstance()
     var database : manager.databaseNamed("mydb", error: nil)

    //Create model
     
var model : MYModel = MYModel()
     
//!!!Exception!!!
     model
.name = "somename"
     model
.database = database      
 
}

}


MYModel.h

#import "CBLModel.h"

@interface MYModel : CBLModel

    @property (copyNSString* name;

@end



MYModel.m

#import "MYModel.h"

@implementation MYModel : CBLModel

    @dynamic name;

@end



What am i doing wrong? 
Thx for your help!

Xcode6
couchbase-lite-1.0.0

Jens Alfke

unread,
Sep 23, 2014, 11:28:28 AM9/23/14
to mobile-c...@googlegroups.com
Hm, the bare -init method doesn't get used on iOS so it probably hasn't been exercised in a while. Capture the backtrace of the exception and file a bug report, please.

> i would like to create my CBLModels in the view layer and persist them later in the database.

IIRC, you can't do that. The bare -init method has to be followed by -setDatabase: before you can do anything with the model object; its property accessors will fail if there's no associated CBLDocument yet. As the comment says, the bare -init method is only supported because NSController objects always instantiate their owned objects with that initializer.

—Jens
Reply all
Reply to author
Forward
0 new messages