Impressia/CoreData/AttachmentDataHandler.swift

39 lines
1.5 KiB
Swift
Raw Normal View History

2023-01-01 18:13:36 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-01 18:13:36 +01:00
//
import Foundation
2023-01-02 08:11:38 +01:00
import CoreData
2023-01-01 18:13:36 +01:00
class AttachmentDataHandler {
2023-01-05 11:55:20 +01:00
public static let shared = AttachmentDataHandler()
private init() { }
2023-01-02 08:11:38 +01:00
func createAttachmnentDataEntity(viewContext: NSManagedObjectContext? = nil) -> AttachmentData {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
2023-01-01 18:13:36 +01:00
return AttachmentData(context: context)
}
2023-10-11 18:42:56 +02:00
func getDownloadedAttachmentData(accountId: String, length: Int, viewContext: NSManagedObjectContext? = nil) -> [AttachmentData] {
let context = viewContext ?? CoreDataHandler.shared.container.viewContext
let fetchRequest = AttachmentData.fetchRequest()
fetchRequest.fetchLimit = length
let sortDescriptor = NSSortDescriptor(key: "statusRelation.id", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
let predicate1 = NSPredicate(format: "statusRelation.pixelfedAccount.id = %@", accountId)
let predicate2 = NSPredicate(format: "data != nil")
fetchRequest.predicate = NSCompoundPredicate.init(type: .and, subpredicates: [predicate1, predicate2])
do {
return try context.fetch(fetchRequest)
} catch {
CoreDataError.shared.handle(error, message: "Error during fetching attachment data (getDownloadedAttachmentData).")
return []
}
}
2023-01-01 18:13:36 +01:00
}