Fix race condition in creating new post

This commit is contained in:
Angelo Stavrow 2021-01-14 13:29:51 -05:00
parent 3b6c7748e8
commit 9b06eb34a8
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 18 additions and 4 deletions

View File

@ -20,8 +20,14 @@ struct ContentView: View {
Spacer()
Button(action: {
withAnimation {
// Un-set the currently selected post
self.model.selectedPost = nil
// Navigate to the Drafts list
self.model.showAllPosts = false
self.model.selectedCollection = nil
}
// Create the new-post managed object
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.createdDate = Date()
managedPost.title = ""
@ -40,10 +46,10 @@ struct ContentView: View {
managedPost.language = languageCode
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
}
withAnimation {
DispatchQueue.main.async {
self.model.showAllPosts = false
self.model.selectedCollection = nil
DispatchQueue.main.asyncAfter(deadline: .now()) {
// Load the new post in the editor
self.model.selectedPost = managedPost
}
}

View File

@ -114,8 +114,14 @@ struct WriteFreely_MultiPlatformApp: App {
private func createNewLocalPost() {
withAnimation {
// Un-set the currently selected post
self.model.selectedPost = nil
// Navigate to the Drafts list
self.model.showAllPosts = false
self.model.selectedCollection = nil
}
// Create the new-post managed object
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.createdDate = Date()
managedPost.title = ""
@ -135,7 +141,9 @@ struct WriteFreely_MultiPlatformApp: App {
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
}
withAnimation {
DispatchQueue.main.asyncAfter(deadline: .now()) {
self.model.selectedPost = managedPost
}
}
}
}