Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions LeanCloudTests/LCUserTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,26 @@ class LCUserTestCase: BaseTestCase {
XCTAssertNil(LCApplication.default.currentUser)
}

func testSaveToLocalAfterSaved() {
let username = UUID().uuidString
let password = UUID().uuidString
let signUpUser = LCUser()
signUpUser.username = username.lcString
signUpUser.password = password.lcString
XCTAssertTrue(signUpUser.signUp()
.isSuccess)
XCTAssertTrue(LCUser.logIn(
username: username,
password: password)
.isSuccess)
let key = "customKey"
let value = UUID().uuidString
try? LCApplication.default.currentUser?.set(key, value: value)
let result = LCApplication.default.currentUser?.save()
XCTAssertNil(result?.error)
LCApplication.default._currentUser = nil
XCTAssertEqual(LCApplication.default.currentUser?[key]?.stringValue, value)
LCUser.logOut()
XCTAssertNil(LCApplication.default.currentUser)
}
}
13 changes: 13 additions & 0 deletions Sources/Foundation/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ open class LCUser: LCObject {
}
}

func trySaveToLocal() {
if self === self.application._currentUser {
self.application.currentUser = self
}
}

// MARK: Sign up

/// Sign up an user synchronously.
Expand Down Expand Up @@ -1466,6 +1472,7 @@ open class LCUser: LCObject {
unsafeObject: [platform.key: authData])
}
ObjectProfiler.shared.updateObject(self, dictionary)
self.trySaveToLocal()
completion(.success)
} catch {
completion(.failure(
Expand Down Expand Up @@ -1547,6 +1554,7 @@ open class LCUser: LCObject {
if let dictionary = response.value as? [String: Any] {
self.authData?.removeValue(forKey: platform.key)
ObjectProfiler.shared.updateObject(self, dictionary)
self.trySaveToLocal()
completion(.success)
} else {
completion(.failure(
Expand All @@ -1557,4 +1565,9 @@ open class LCUser: LCObject {
}
}
}

override func objectDidSave() {
super.objectDidSave()
self.trySaveToLocal()
}
}