2016-11-05 76 views
0

说我本来存储通过使用锁匠一些数据:更新部分3

    do 
        { 

         try Locksmith.saveData(data: ["a": a_data, "b":b_data, "c":c_data, "d":d_data], forUserAccount: "user") //store it 
         print("Data successfully Saved.") 
        } catch ... 

有没有可能回去以后,只有不覆盖收纳于现有的数据更新的特定字段钥匙扣。例如,如果我最初存储了a,b,c和d。当我更新字段b相:

try Locksmith.updateData(["b": "new"], forUserAccount: "user") 

我会覆盖A,B,C,d,只有新的数据(即A,B,C,d - > new_b)或将锁匠更新功能只更新特定领域。我应该提到,我试图在文档中找到答案,但似乎无法找到答案,除非我忽略了它。在这种情况下,我很抱歉。尽管如此,任何帮助表示赞赏。

感谢

回答

1

我相信文档是很清楚,可使用固定Locksmith.updateData(_:, forUserAccount:)方法取代任何给定的数据的用户。我从matthewpalmer/Locksmith [重点煤矿]引用

更新数据

以及替换现有的数据,如果不存在

它这个数据写入 钥匙扣
try Locksmith.updateData(["some key": "another value"], 
         forUserAccount: "myUserAccount") 

我们可以通过查看以下非失败的测试(Locksmith/Tests/LocksmithTests.swift)验证这种说法:

// .... 

let userAccount = "myUser" 

func testStaticMethods() { 
    // ...  

    let otherData: TestingDictionaryType = ["something": "way different"] 

    // ... 

    let updatedData = ["this update": "brings the ruckus"] 
    try! Locksmith.updateData(data: updatedData, forUserAccount: userAccount, inService: service) 

    let loaded3 = Locksmith.loadDataForUserAccount(userAccount: userAccount, inService: service)! as! TestingDictionaryType 

    // NOTE: this assert checks that the loaded data equals the 
    //  updated data (_not_ 'otherData' updated by 'updatedData') 
    XCTAssertEqual(loaded3, updatedData) 

    // ... 
} 

其中静态Locksmith.updateData(_:, forUserAccount:)方法显然取代定用户帐户的数据。


如果你想简单的子数据添加到现有用户的数据,你可以自己实现:

  1. 给定用户帐户

    let currentData = Locksmith.loadDataForUserAccount(userAccount: userAccount) 
    
  2. 读数据
  3. 手动更新数据字典。有很多SO线程解释这一步骤,参见例如。

  4. 用户Locksmith.saveData(_:, forUserAccount:)静态方法来保存更新后的数据为给定的用户帐户。

+0

感谢您的简单解释! –

+0

@RobertHerrera乐意帮忙! – dfri