Search code examples
iosswiftcouchbase-lite

Saving Offline data into CB Lite 2.0


issue : ( CB Lite 2.0 , Swift)

Registration screen which has name, email and dob and want to save in couchbase lite. everytime user-filled form with these fields I want to store in local CB Lite DB After that i want to fetch all record store in the cb lite. Issue is :

Created a document

stored name , emal and dob in swift dictionary and tried to save and it stores but everytime i fetch it shows only one , not other user information.

Kinldy help me on this.

Raised Query on Couchbase Forum :

https://forums.couchbase.com/t/saving-offline-data-into-cb-lite-2-0/17877?u=st.shubh.tiwari


Solution

  • This is the way i called by taking one sample notification and checked its working :

    let dict = [
                        "alert": "push notification.. (5)",
                        "badge": 1,
                        "sound": "default"
                    ] as [String : Any]
    
                var notificationArray = [Dictionary<String,Any>]()
    
                let notiDict = CouchDBHelper.fetchDocumentByDocId(documentID: Constants.notificationDocument)
    
                if let notiArray = notiDict["notification"] as? [Dictionary<String,Any>] {
                    notificationArray = notiArray
                }
                notificationArray.append(dict)
    
                CCouchDBHelper.insertNotificationData(data: notificationArray, doc_id: Constants.notificationDocument)
                objNotifcationSource = notificationArray
                objNotificationTable.reloadData()
    
    
    
           public func insertNotificationData(data:Any?,doc_id:String)
            {
                //let sharedInstance = CouchbaseAdapter.sharedInstance
                let doc = MutableDocument(id: doc_id) 
    
                doc.setValue(data, forKey: "notification")
    
                do {
                    try database.saveDocument(doc)
                    //try sharedInstance.database.saveDocument(doc)
                    print("Inserted Record :", data as! NSArray)
                } catch let error as NSError {
                    print("Error in saving",error)
                }
    
                fetchDocumentByDocId(documentID: doc_id)
            }