Ensure we don't nil out the collection alias on updating from server

This commit is contained in:
Angelo Stavrow 2020-09-10 12:30:02 -04:00
parent a20b2911b7
commit 089a12dcb2
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -112,7 +112,12 @@ extension WriteFreelyModel {
DispatchQueue.main.async {
self.selectedPost = post
}
loggedInClient.getPost(byId: postId, completion: updateFromServerHandler)
if let postCollectionAlias = post.collectionAlias,
let postSlug = post.slug {
loggedInClient.getPost(bySlug: postSlug, from: postCollectionAlias, completion: updateFromServerHandler)
} else {
loggedInClient.getPost(byId: postId, completion: updateFromServerHandler)
}
}
}
@ -280,12 +285,15 @@ private extension WriteFreelyModel {
}
func updateFromServerHandler(result: Result<WFPost, Error>) {
// NOTE:
// The API does not return a collection alias, so we take care not to overwrite the
// cached post's collection alias with the 'nil' value from the fetched post.
// See: https://github.com/writeas/writefreely-swift/issues/20
do {
let fetchedPost = try result.get()
guard let cachedPost = self.selectedPost else { return }
cachedPost.appearance = fetchedPost.appearance
cachedPost.body = fetchedPost.body
cachedPost.collectionAlias = fetchedPost.collectionAlias
cachedPost.createdDate = fetchedPost.createdDate
cachedPost.language = fetchedPost.language
cachedPost.postId = fetchedPost.postId