Cache lists in CoreData
This commit is contained in:
parent
975be17d13
commit
795146cde4
|
@ -317,6 +317,17 @@ class MastodonController: ObservableObject {
|
|||
DispatchQueue.main.async {
|
||||
self.lists = lists.sorted(using: SemiCaseSensitiveComparator.keyPath(\.title))
|
||||
}
|
||||
let context = self.persistentContainer.backgroundContext
|
||||
context.perform {
|
||||
for list in lists {
|
||||
if let existing = try? context.fetch(ListMO.fetchRequest(id: list.id)).first {
|
||||
existing.updateFrom(apiList: list)
|
||||
} else {
|
||||
_ = ListMO(apiList: list, context: context)
|
||||
}
|
||||
}
|
||||
self.persistentContainer.save(context: context)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
//
|
||||
// ListMO.swift
|
||||
// Tusker
|
||||
//
|
||||
// Created by Shadowfacts on 12/20/22.
|
||||
// Copyright © 2022 Shadowfacts. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
import Pachyderm
|
||||
|
||||
@objc(ListMO)
|
||||
public final class ListMO: NSManagedObject {
|
||||
|
||||
@nonobjc public class func fetchRequest() -> NSFetchRequest<ListMO> {
|
||||
return NSFetchRequest(entityName: "List")
|
||||
}
|
||||
|
||||
@nonobjc public class func fetchRequest(id: String) -> NSFetchRequest<ListMO> {
|
||||
let req = NSFetchRequest<ListMO>(entityName: "List")
|
||||
req.predicate = NSPredicate(format: "id = %@", id)
|
||||
return req
|
||||
}
|
||||
|
||||
@NSManaged public var id: String
|
||||
@NSManaged public var title: String
|
||||
|
||||
}
|
||||
|
||||
extension ListMO {
|
||||
convenience init(apiList list: List, context: NSManagedObjectContext) {
|
||||
self.init(context: context)
|
||||
self.updateFrom(apiList: list)
|
||||
}
|
||||
|
||||
func updateFrom(apiList list: List) {
|
||||
self.id = list.id
|
||||
self.title = list.title
|
||||
}
|
||||
}
|
|
@ -46,6 +46,15 @@
|
|||
<attribute name="name" attributeType="String"/>
|
||||
<attribute name="url" attributeType="URI"/>
|
||||
</entity>
|
||||
<entity name="List" representedClassName="ListMO" syncable="YES">
|
||||
<attribute name="id" optional="YES" attributeType="String"/>
|
||||
<attribute name="title" optional="YES" attributeType="String"/>
|
||||
<uniquenessConstraints>
|
||||
<uniquenessConstraint>
|
||||
<constraint value="id"/>
|
||||
</uniquenessConstraint>
|
||||
</uniquenessConstraints>
|
||||
</entity>
|
||||
<entity name="Relationship" representedClassName="RelationshipMO" syncable="YES">
|
||||
<attribute name="accountID" optional="YES" attributeType="String"/>
|
||||
<attribute name="blocking" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
|
||||
|
@ -121,5 +130,6 @@
|
|||
<memberEntity name="Relationship"/>
|
||||
<memberEntity name="Status"/>
|
||||
<memberEntity name="TimelineState"/>
|
||||
<memberEntity name="List"/>
|
||||
</configuration>
|
||||
</model>
|
Loading…
Reference in New Issue