How does the CBLModel work in swift?

134 views
Skip to first unread message

Karel-Jan Van Haute

unread,
Oct 15, 2014, 3:44:44 AM10/15/14
to mobile-c...@googlegroups.com
Can someone provide me with a small example on how to implement the CBLModel in swift?

Karel-Jan Van Haute

unread,
Oct 15, 2014, 5:37:32 AM10/15/14
to mobile-c...@googlegroups.com
So how to do this in Swift

@interface Note : CBLModel
@property (copy) NSString* text;
@property NSDate* created;
@property bool checked;    // bool not BOOL! See below for why
@end

And here's the implementation of the class:

@implementation Note
@dynamic text, created, checked; // marks these as persistent
@end

Jens Alfke

unread,
Oct 15, 2014, 11:29:54 AM10/15/14
to mobile-c...@googlegroups.com

On Oct 15, 2014, at 12:44 AM, Karel-Jan Van Haute <kare...@gmail.com> wrote:

Can someone provide me with a small example on how to implement the CBLModel in swift?

Search the list archives for "swift" and you'll find a few prior threads about using CBLModel, including the syntax for declaring dynamic properties in Swift.

I have only had time to do a little bit in Swift; I ported Grocery Sync but haven't yet gotten around to working with CBLModel in it.

—Jens

Karel-Jan Van Haute

unread,
Oct 15, 2014, 11:40:00 AM10/15/14
to mobile-c...@googlegroups.com
Ok I think I have it. 
So I will post my solution here for the lost ones.
I'm not sure this is the right way to do it. But it works for now.

I've made a general Model that adds created date and the last modified date.

import Foundation

typealias CBLDoc = CBLDocument

class Model: CBLModel{

   @NSManaged var created: NSDate

   @NSManaged var last_modified: NSDate

   init(inDatabase database: CBLDatabase, ofType type: String){

       super.init(document: self.document)

       self.database = database

       self.created = NSDate(timeIntervalSinceNow: 0)

       self.last_modified = NSDate(timeIntervalSinceNow: 0)

       self.setValue(type, ofProperty: "type")

   }

   override init(document: CBLDoc) {

       super.init(document: document)

   }

}


And then you have a Person class that inherits from the general Model

import Foundation

class Person: Model{

   @NSManaged var owner: String

   @NSManaged var nickname: String

   @NSManaged var status: String

   @NSManaged var testing: String

   let type = "person"

   init(inDatabase database: CBLDatabase, withNickName name: String, owner: String){

       super.init(inDatabase: database, ofType: type)

       self.owner = owner

       self.nickname = name

       self.status = "active"

   }

   override init(document: CBLDoc) {

       super.init(document: document)
   
}

}


I hope this is the way to go.

Jens Alfke

unread,
Oct 15, 2014, 11:53:04 AM10/15/14
to mobile-c...@googlegroups.com

> init(inDatabase database: CBLDatabase, ofType type: String){
>
> super.init(document: self.document)

That's not the right inherited initializer. (In particular, it should be a red flag that you're calling a method on self before calling the superclass initializer. I'm surprised that's not a compile error.)

The right initializer to call would be
super.init(newDocumentInDatabase: database)

And then don't set self.database.

> override init(document: CBLDoc) {

This should be unnecessary, since it has no effect (just calls super).

—Jens
Reply all
Reply to author
Forward
0 new messages