objective-ctouchdb

TouchDB saves the attachment but does not save the property


I found that my CouchModel (TSCAssetUploadDO) saves the attachment but does not save the property.

    TSCAssetUploadDO *assetDO = [[TSCAssetUploadDO alloc] initWithNewDocumentInDatabase: _localAssetUploadDatabase];
    NSData *data = UIImageJPEGRepresentation(contact.avatar, 1.0); // may need to resize.
    NSString *attachmentName = [docID stringByAppendingString:@".jpg"];
    [assetDO createAttachmentWithName:attachmentName type:@"image/jpeg" body:data];
    assetDO.relatedDocID = docID;
    assetDO.docType = @"contact";
    RESTOperation *op2 = [assetDO save];
    //[op2 wait]; originally thought that making it sync may work
    [op2 onCompletion:^{
        if (op2.error) NSLog(@"ERROR [TOUCHDB] %@", op2.error);
        else
        {
            NSLog(@"YES! it saved");
        }
        if (completionBlock)
        {
            completionBlock(op2.error, contact);
        }
        //[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationDataUpdated object:nil];
    }];

This is resulting to a document like this ( with saved attachment, no property like docType and relatedDocID though )

{
 "_id": "D50ED630-34ED-4A02-A9C8-204E79A0648B",
 "_rev": "1-b0ce9eaa1fb2f86dc9ae619e27ffe1ea",
 "_attachments": {
   "QSPNGC665.jpg": {
       "content_type": "image/jpeg",
       "revpos": 1,
       "digest": "md5-u9V0rgoSRN5cUW2T3xh0hw==",
       "length": 117500,
       "stub": true
   }
 }
}

Below is the CouchModel that I just used.

@interface TSCAssetUploadDO: CouchModel
  @property(nonatomic,retain) NSString *relatedDocID;
  @property(nonatomic,retain) NSString *docType;//entity or contact
  @property bool *toProcess;
  @property (retain) NSDate* created_at;
@end

@implementation TSCAssetUploadDO

  - (NSDictionary*) propertiesToSave {
     // Initialize created_at the first time the document is saved:
     if (self.created_at == nil)
      self.created_at = [NSDate date];
     return [super propertiesToSave];
     }
 @end

Is there anything that I did wrong?


Solution

  • Solved my issue. need to add @dynamic

    @implementation TSCAssetUploadDO
       @dynamic relatedDocID, docType, toProcess, created_at;
       <... rest of the code >
    @end