I am using couchbase lite in my Mac app. I have a source list that displays categories in a tree data structure. Each category is an object that has a title and a children property that's a mutable array.
The model header looks something like this:
#import <CouchbaseLite/CouchbaseLite.h>
@interface SBCategory : CBLModel <NSPasteboardWriting, NSPasteboardReading>
@property(copy) NSString* uuid;
@property(copy) NSString* title;
@property(copy) NSDate* created_at;
@property(assign) BOOL isHeader;
@property (readonly, copy) NSMutableArray* children;
@end
I'm not using a nstreecontroller, just the array of objects mentioned above. I've really been wracking my brain as to how to store this in the database. Should I store each object individually? Should I try to serialize the whole structure to json and just store that? Should I be using a tree controller and does if offer helper methods for saving data? Advice on this is much appreciated. I'd really like to hear from some of the experts on SOF how they'd do this.
You may store each model individually in a CBL document. You only need to keep a reference on the root model or to keep its documentId.
You may also store the full tree inside a single document using nested models. You will have to change your declaration to:
@interface SBCategory : NSObject <CBLJSONEncoding, NSPasteboardWriting, NSPasteboardReading>
To see examples on the use of CBLJSONEncoding, you can download CBL sources. However, in this last case, any change to one of your nested model will cause your full tree of models to be rewritten. On the contrary, if you modify the tree from multiples devices at the same time, that will be easier to ensure consistency as there is a single document. CBLJSONEncoding is more tricky but very useful in NoSql databases.