Skip to content

Commit

Permalink
Revert most of PR #632 to fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
mplorentz committed Dec 1, 2023
1 parent 4e87ba4 commit 95726c9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/SDWebImage/SDWebImage.git",
"state" : {
"revision" : "36e484b317522667a4b2de9b50daaa01dfa30809",
"version" : "5.18.0"
"revision" : "1b9a2e902cbde5fdf362faa0f4fd76ea74d74305",
"version" : "5.18.5"
}
},
{
"identity" : "sdwebimageswiftui",
"kind" : "remoteSourceControl",
"location" : "https://github.com/SDWebImage/SDWebImageSwiftUI",
"state" : {
"revision" : "e837c37d45449fbd3b4745c10c5b5274e73edead",
"version" : "2.2.3"
"revision" : "aee64ef39b570c44ccf0f884c440fc6494a23c76",
"version" : "2.2.5"
}
},
{
Expand All @@ -113,8 +113,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/getsentry/sentry-cocoa.git",
"state" : {
"revision" : "d062d9b31ccabdec706134f2216476d1996caf11",
"version" : "8.10.0"
"revision" : "a3e15ba9fd6c8efc6515ad9d6f3337bb2dc2e1e3",
"version" : "8.17.0"
}
},
{
Expand Down Expand Up @@ -158,8 +158,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
"revision" : "a902f1823a7ff3c9ab2fba0f992396b948eda307",
"version" : "1.0.5"
}
},
{
Expand All @@ -180,6 +180,15 @@
"version" : "0.1.4"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "99d066e29effa8845e4761dd3f2f831edfdf8925",
"version" : "1.0.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
Expand All @@ -194,17 +203,26 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "3db5c4aeee8100d2db6f1eaf3864afdad5dc68fd",
"version" : "2.59.0"
"revision" : "702cd7c56d5d44eeba73fdf83918339b26dc855c",
"version" : "2.62.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "fb70a0f5e984f23be48b11b4f1909f3bee016178",
"version" : "1.19.1"
"revision" : "798c962495593a23fdea0c0c63fd55571d8dff51",
"version" : "1.20.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "3bd9004b9d685ed6b629760fc84903e48efec806",
"version" : "1.29.0"
}
},
{
Expand All @@ -221,8 +239,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "e7403c35ca6bb539a7ca353b91cc2d8ec0362d58",
"version" : "1.19.0"
"revision" : "ebf8b9c365a6ce043bf6e6326a04b15589bd285e",
"version" : "1.20.0"
}
},
{
Expand Down
24 changes: 4 additions & 20 deletions Nos/Models/Author+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,13 @@ enum AuthorError: Error {

@discardableResult
class func findOrCreate(by pubKey: HexadecimalString, context: NSManagedObjectContext) throws -> Author {
@Dependency(\.persistenceController) var persistenceController
@Dependency(\.crashReporting) var crashReporting

if let author = try? Author.find(by: pubKey, context: context) {
return author
} else {
/// Always create authors in the creationContext first to make sure we never end up with two identical
/// Authors in different contexts with the same objectID, because this messes up SwiftUI's observation
/// of changes.
let creationContext = persistenceController.creationContext
let objectID = try creationContext.performAndWait {
let author = Author(context: creationContext)
author.hexadecimalPublicKey = pubKey
author.muted = false
try creationContext.save()
return author.objectID
}
guard let fetchedAuthor = context.object(with: objectID) as? Author else {
let error = AuthorError.coreData
crashReporting.report(error)
throw error
}
return fetchedAuthor
let author = Author(context: context)
author.hexadecimalPublicKey = pubKey
author.muted = false
return author
}
}

Expand Down
49 changes: 8 additions & 41 deletions Nos/Models/Event+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,28 +488,11 @@ public class Event: NosManagedObject {
}
return existingEvent
} else {
@Dependency(\.crashReporting) var crashReporting
@Dependency(\.persistenceController) var persistenceController

/// Always create events in the creationContext first to make sure we never end up with two identical
/// Events in different contexts with the same objectID, because this messes up SwiftUI's observation
/// of changes.
let creationContext = persistenceController.creationContext
let objectID = try creationContext.performAndWait {
let event = Event(context: creationContext)
event.identifier = jsonEvent.id
try creationContext.save()
return event.objectID
}
guard let fetchedEvent = context.object(with: objectID) as? Event else {
let error = EventError.coreData
crashReporting.report(error)
throw error
}

fetchedEvent.receivedAt = .now
try fetchedEvent.hydrate(from: jsonEvent, relay: relay, in: context)
return fetchedEvent
let event = Event(context: context)
event.identifier = jsonEvent.id
event.receivedAt = .now
try event.hydrate(from: jsonEvent, relay: relay, in: context)
return event
}
}

Expand All @@ -524,25 +507,9 @@ public class Event: NosManagedObject {
if let existingEvent = try context.fetch(Event.event(by: id)).first {
return existingEvent
} else {
@Dependency(\.crashReporting) var crashReporting
@Dependency(\.persistenceController) var persistenceController

/// Always create events in the creationContext first to make sure we never end up with two identical
/// Events in different contexts with the same objectID, because this messes up SwiftUI's observation
/// of changes.
let creationContext = persistenceController.creationContext
let objectID = try creationContext.performAndWait {
let event = Event(context: creationContext)
event.identifier = id
try creationContext.save()
return event.objectID
}
guard let fetchedEvent = context.object(with: objectID) as? Event else {
let error = EventError.coreData
crashReporting.report(error)
throw error
}
return fetchedEvent
let event = Event(context: context)
event.identifier = id
return event
}
}

Expand Down
6 changes: 0 additions & 6 deletions Nos/Models/Persistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ class PersistenceController {
container.viewContext
}

/// A context to synchronize creation of Events and Authors so we don't end up with duplicates.
lazy var creationContext = {
newBackgroundContext()
}()

/// A context for parsing Nostr events from relays.
lazy var parseContext = {
self.newBackgroundContext()
Expand Down Expand Up @@ -86,7 +81,6 @@ class PersistenceController {
}
setUp()
viewContext.reset()
creationContext = newBackgroundContext()
backgroundViewContext = newBackgroundContext()
parseContext = newBackgroundContext()
}
Expand Down

0 comments on commit 95726c9

Please sign in to comment.