2020-02-10 17:30:11 -08:00
|
|
|
//
|
|
|
|
|
// FetchAppIDsOperation.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 1/27/20.
|
|
|
|
|
// Copyright © 2020 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
import AltStoreCore
|
2020-02-10 17:30:11 -08:00
|
|
|
import AltSign
|
|
|
|
|
import Roxas
|
|
|
|
|
|
|
|
|
|
@objc(FetchAppIDsOperation)
|
2023-01-04 09:52:12 -05:00
|
|
|
final class FetchAppIDsOperation: ResultOperation<([AppID], NSManagedObjectContext)>
|
2020-02-10 17:30:11 -08:00
|
|
|
{
|
2020-03-06 17:08:35 -08:00
|
|
|
let context: AuthenticatedOperationContext
|
|
|
|
|
let managedObjectContext: NSManagedObjectContext
|
2020-02-10 17:30:11 -08:00
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
init(context: AuthenticatedOperationContext, managedObjectContext: NSManagedObjectContext = DatabaseManager.shared.persistentContainer.newBackgroundContext())
|
2020-02-10 17:30:11 -08:00
|
|
|
{
|
|
|
|
|
self.context = context
|
2020-03-06 17:08:35 -08:00
|
|
|
self.managedObjectContext = managedObjectContext
|
2020-02-10 17:30:11 -08:00
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
|
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
if let error = self.context.error
|
2020-02-10 17:30:11 -08:00
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard
|
2020-03-06 17:08:35 -08:00
|
|
|
let team = self.context.team,
|
|
|
|
|
let session = self.context.session
|
2024-11-09 14:35:18 +05:30
|
|
|
else {
|
|
|
|
|
return self.finish(.failure(OperationError.invalidParameters("FetchAppIDsOperation.main: self.context.team or self.context.session is nil")))
|
|
|
|
|
}
|
2020-02-10 17:30:11 -08:00
|
|
|
|
|
|
|
|
ALTAppleAPI.shared.fetchAppIDs(for: team, session: session) { (appIDs, error) in
|
2020-03-06 17:08:35 -08:00
|
|
|
self.managedObjectContext.perform {
|
2020-02-10 17:30:11 -08:00
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
let fetchedAppIDs = try Result(appIDs, error).get()
|
|
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
guard let team = Team.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(Team.identifier), team.identifier), in: self.managedObjectContext) else { throw OperationError.notAuthenticated }
|
2020-02-10 17:30:11 -08:00
|
|
|
|
|
|
|
|
let fetchedIdentifiers = fetchedAppIDs.map { $0.identifier }
|
|
|
|
|
|
|
|
|
|
let deletedAppIDsRequest = AppID.fetchRequest() as NSFetchRequest<AppID>
|
|
|
|
|
deletedAppIDsRequest.predicate = NSPredicate(format: "%K == %@ AND NOT (%K IN %@)",
|
|
|
|
|
#keyPath(AppID.team), team,
|
|
|
|
|
#keyPath(AppID.identifier), fetchedIdentifiers)
|
|
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
let deletedAppIDs = try self.managedObjectContext.fetch(deletedAppIDsRequest)
|
|
|
|
|
deletedAppIDs.forEach { self.managedObjectContext.delete($0) }
|
2020-02-10 17:30:11 -08:00
|
|
|
|
2020-03-06 17:08:35 -08:00
|
|
|
let appIDs = fetchedAppIDs.map { AppID($0, team: team, context: self.managedObjectContext) }
|
|
|
|
|
self.finish(.success((appIDs, self.managedObjectContext)))
|
2020-02-10 17:30:11 -08:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|