Couchbase Lite
Objective-C API for iOS and Mac OS
CBLModel.h
Go to the documentation of this file.
1 //
2 // CBLModel.h
3 // CouchbaseLite
4 //
5 // Created by Jens Alfke on 8/26/11.
6 // Copyright (c) 2011-2013 Couchbase, Inc. All rights reserved.
7 //
8 
9 #import "CBLDynamicObject.h"
10 #import "CBLDocument.h"
11 
14 
15 
16 NS_REQUIRES_PROPERTY_DEFINITIONS // Don't let compiler auto-synthesize properties in subclasses
24 @interface CBLModel : CBLDynamicObject <CBLDocumentModel>
25 
30 + (nullable instancetype) modelForDocument: (CBLDocument*)document;
31 
34 + (instancetype) modelForNewDocumentInDatabase: (CBLDatabase*)database;
35 
36 // You cannot create CBLModel instances with -init. Use the factory class methods instead.
37 - (instancetype) init NS_UNAVAILABLE;
38 
40 @property (readonly, strong, nullable) CBLDocument* document;
41 
45 @property (retain, nullable) CBLDatabase* database;
46 
48 @property (readonly) bool isNew;
49 
50 #pragma mark - SAVING:
51 
54 - (BOOL) save: (NSError**)outError;
55 
58 @property (nonatomic) bool autosaves;
59 
62 @property (readonly) NSTimeInterval autosaveDelay;
63 
65 @property (readonly) bool needsSave;
66 
70 - (NSDictionary*) propertiesToSave;
71 
73 - (void) revertChanges;
74 
77 - (BOOL) deleteDocument: (NSError**)outError;
78 
81 @property (readonly) NSTimeInterval timeSinceExternallyChanged;
82 
90 + (BOOL) saveModels: (CBLArrayOf(CBLModel*)*)models
91  error: (NSError**)outError;
92 
94 - (void) markExternallyChanged;
95 
96 #pragma mark - PROPERTIES & ATTACHMENTS:
97 
100 - (nullable id) getValueOfProperty: (NSString*)property;
101 
104 - (BOOL) setValue: (nullable id)value
105  ofProperty: (NSString*)property;
106 
107 
120 - (CBLArrayOf(CBLModel*)*) findInverseOfRelation: (NSString*)relation
121  fromClass: (nullable Class)fromClass;
122 
123 
126 @property (readonly, nullable) CBLArrayOf(NSString*)* attachmentNames;
127 
129 - (nullable CBLAttachment*) attachmentNamed: (NSString*)name;
130 
137 - (void) setAttachmentNamed: (NSString*)name
138  withContentType: (NSString*)mimeType
139  content: (NSData*)content;
140 
150 - (void) setAttachmentNamed: (NSString*)name
151  withContentType: (NSString*)mimeType
152  contentURL: (NSURL*)fileURL;
153 
156 - (void) removeAttachmentNamed: (NSString*)name;
157 
158 
159 #pragma mark - PROTECTED (FOR SUBCLASSES TO OVERRIDE)
160 
161 
162 /* Called when the model's initializer is called when the model object is created. You should override this if you need to initialize
163  any ivars or perform custom initialization when the model object is created.
164  */
165 - (void)awakeFromInitializer;
166 
169 - (nullable NSString*) idForNewDocumentInDatabase: (CBLDatabase*)db;
170 
173 - (void) didLoadFromDocument;
174 
177 - (CBLDatabase*) databaseForModelProperty: (NSString*)propertyName;
178 
181 - (void) markNeedsSave;
182 
185 - (void) willSave: (nullable NSSet*)changedPropertyNames;
186 
189 - (NSDictionary*) propertiesToSaveForDeletion;
190 
203 + (nullable Class) itemClassForArrayProperty: (NSString*)property;
204 
218 + (nullable NSString*) inverseRelationForArrayProperty: (NSString*)property;
219 
223 @property (copy, nonatomic, nullable) NSString* type;
224 
225 @end
226 
227 
228 
231 
233 @property (readonly, nullable) CBLArrayOf(CBLModel*)* unsavedModels;
234 
236 - (BOOL) saveAllModels: (NSError**)outError;
237 
241 - (BOOL) autosaveAllModels: (NSError**)outError;
242 
243 @end
244 
245 
instancetype NS_UNAVAILABLE()
#define NS_ASSUME_NONNULL_BEGIN
Definition: CBLBase.h:20
void markNeedsSave()
Marks the model as having unsaved content, ensuring that it will get saved after a short interval (if...
A CouchbaseLite database.
Definition: CBLDatabase.h:38
void awakeFromInitializer()
#define nullable
Definition: CBLBase.h:22
bool isNew
Is this model new, never before saved?
Definition: CBLModel.h:48
void revertChanges()
Removes any changes made to properties and attachments since the last save.
A CouchbaseLite document (as opposed to any specific revision of it.)
Definition: CBLDocument.h:16
void didLoadFromDocument()
Called when the model&#39;s properties are reloaded from the document.
bool needsSave
Does this model have unsaved changes?
Definition: CBLModel.h:65
NSDictionary * propertiesToSaveForDeletion()
If you want properties to be saved in the document when it&#39;s deleted (in addition to the required "_d...
NSDictionary * propertiesToSave()
The document&#39;s current properties (including unsaved changes) in externalized JSON format...
#define NS_ASSUME_NONNULL_END
Definition: CBLBase.h:21
Generic model class for CouchbaseLite documents.
Definition: CBLModel.h:24
CBLDatabase * database
The database the item&#39;s document belongs to.
Definition: CBLModel.h:45
NSString * type
The type of document.
Definition: CBLModel.h:223
NSTimeInterval autosaveDelay
How long to wait after the first change before auto-saving, if autosaves is true. ...
Definition: CBLModel.h:62
A generic class with runtime support for dynamic properties.
Definition: CBLDynamicObject.h:15
CBLArrayOf(NSNumber *) *CBLGeoPointToCoordPair(CBLGeoPoint pt)
Converts a point to a JSON-compatible array of two coordinates.
void markExternallyChanged()
Resets the timeSinceExternallyChanged property to zero.
NSTimeInterval timeSinceExternallyChanged
The time interval since the document was last changed externally (e.g.
Definition: CBLModel.h:81
A binary attachment to a document revision.
Definition: CBLAttachment.h:18
bool autosaves
Should changes be saved back to the database automatically? Defaults to NO, requiring you to call -sa...
Definition: CBLModel.h:58
CBLDatabase methods for use with CBLModel.
Definition: CBLModel.h:230
CBLDocument * document
The document this item is associated with.
Definition: CBLModel.h:40