fix: the Core Data thread-safe issue

This commit is contained in:
sunxiaojian 2021-04-29 16:20:18 +08:00
parent ee5e2bde1f
commit 9768721247
2 changed files with 14 additions and 14 deletions

View File

@ -28,15 +28,15 @@ extension APIService {
) )
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Notification]>, Error> in .flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Notification]>, Error> in
let log = OSLog.api let log = OSLog.api
if query.maxID == nil {
let requestMastodonNotificationRequest = MastodonNotification.sortedFetchRequest
requestMastodonNotificationRequest.predicate = MastodonNotification.predicate(domain: domain, userID: userID)
let oldNotifications = self.backgroundManagedObjectContext.safeFetch(requestMastodonNotificationRequest)
oldNotifications.forEach { notification in
self.backgroundManagedObjectContext.delete(notification)
}
}
return self.backgroundManagedObjectContext.performChanges { return self.backgroundManagedObjectContext.performChanges {
if query.maxID == nil {
let requestMastodonNotificationRequest = MastodonNotification.sortedFetchRequest
requestMastodonNotificationRequest.predicate = MastodonNotification.predicate(domain: domain, userID: userID)
let oldNotifications = self.backgroundManagedObjectContext.safeFetch(requestMastodonNotificationRequest)
oldNotifications.forEach { notification in
self.backgroundManagedObjectContext.delete(notification)
}
}
response.value.forEach { notification in response.value.forEach { notification in
let (mastodonUser, _) = APIService.CoreData.createOrMergeMastodonUser(into: self.backgroundManagedObjectContext, for: nil, in: domain, entity: notification.account, userCache: nil, networkDate: Date(), log: log) let (mastodonUser, _) = APIService.CoreData.createOrMergeMastodonUser(into: self.backgroundManagedObjectContext, for: nil, in: domain, entity: notification.account, userCache: nil, networkDate: Date(), log: log)
var status: Status? var status: Status?

View File

@ -11,13 +11,13 @@ import Combine
// MARK: - Account credentials // MARK: - Account credentials
extension Mastodon.API.Account { extension Mastodon.API.Account {
static func acceptFollowRequestEndpointURL(domain: String, userID: String) -> URL { static func acceptFollowRequestEndpointURL(domain: String, userID: Mastodon.Entity.Account.ID) -> URL {
return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("follow_requests") return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("follow_requests")
.appendingPathComponent(userID) .appendingPathComponent(userID)
.appendingPathComponent("authorize") .appendingPathComponent("authorize")
} }
static func rejectFollowRequestEndpointURL(domain: String, userID: String) -> URL { static func rejectFollowRequestEndpointURL(domain: String, userID: Mastodon.Entity.Account.ID) -> URL {
return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("follow_requests") return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("follow_requests")
.appendingPathComponent(userID) .appendingPathComponent(userID)
.appendingPathComponent("reject") .appendingPathComponent("reject")
@ -34,12 +34,12 @@ extension Mastodon.API.Account {
/// - session: `URLSession` /// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com" /// - domain: Mastodon instance domain. e.g. "example.com"
/// - userID: ID of the account in the database /// - userID: ID of the account in the database
/// - authorization: App token /// - authorization: User token
/// - Returns: `AnyPublisher` contains `Relationship` nested in the response /// - Returns: `AnyPublisher` contains `Relationship` nested in the response
public static func acceptFollowRequest( public static func acceptFollowRequest(
session: URLSession, session: URLSession,
domain: String, domain: String,
userID: String, userID: Mastodon.Entity.Account.ID,
authorization: Mastodon.API.OAuth.Authorization authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Relationship>, Error> { ) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Relationship>, Error> {
let request = Mastodon.API.post( let request = Mastodon.API.post(
@ -66,12 +66,12 @@ extension Mastodon.API.Account {
/// - session: `URLSession` /// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com" /// - domain: Mastodon instance domain. e.g. "example.com"
/// - userID: ID of the account in the database /// - userID: ID of the account in the database
/// - authorization: App token /// - authorization: User token
/// - Returns: `AnyPublisher` contains `Relationship` nested in the response /// - Returns: `AnyPublisher` contains `Relationship` nested in the response
public static func rejectFollowRequest( public static func rejectFollowRequest(
session: URLSession, session: URLSession,
domain: String, domain: String,
userID: String, userID: Mastodon.Entity.Account.ID,
authorization: Mastodon.API.OAuth.Authorization authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Relationship>, Error> { ) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Relationship>, Error> {
let request = Mastodon.API.post( let request = Mastodon.API.post(