将异步核心数据移动到扩展名

时间:2018-06-08 18:00:43

标签: ios swift core-data extension-methods

我无法在我的应用程序(Swift 4.2)中找到使这个func可重用的方法,我想通过扩展添加到UIViewController,但我认为不允许以某种方式。即使使用inout问题似乎也没有解决,当移动和操作它在应用程序的其余部分中“可重用”时,我得到了大部分的两个错误:

如果我尝试设置方法参数“asyncFetch:NSAsynchronousFetchRequest”我去错误无法分配给值:'asyncFetch'是'let'常量,如果我尝试为数组添加参数则相同错误

当我尝试使func返回一个NSmanagedObject数组时发生第二个错误,我不能使用guard语句,因为它们“返回”是为了返回func本身(更多,因为是异步,不能返回一个数组填充,但只是空的)

    func asyncFetchAllEntities() {

        print("Fetching all entities...")

        asyncPersonEntityArray.removeAll(keepingCapacity: true)


        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
        let managedContext = appDelegate.persistentContainer.viewContext

        let personFetch : NSFetchRequest<Person> = Person.fetchRequest()
        //here you can add some predicate

        //this block makes the fetch Asynch - start :
        //************************************************************************************
        asyncFetchRequest = NSAsynchronousFetchRequest<Person>(fetchRequest: personFetch) {
            [unowned self] (result: NSAsynchronousFetchResult) in
            guard let allPersonsResult = result.finalResult else {
                return
            }

            self.asyncPersonEntityArray = allPersonsResult

            //here you can do what you need - start :
            //************************************

            do {
                self.asyncPersonEntityArray = try managedContext.fetch(personFetch)
                if self.asyncPersonEntityArray.count > 0 {
                    print("OK - asyncFetchAllEntities: - entity model is not empty!") //
                } else {
//                    self.myPersErrorMessage(errorMessage: "WARNING! - no entitites error 1")
                    print("WARNING! - no entitites error 1")
                }
            } catch let error as NSError {
                print("WARNING! - asyncFetchAllEntities: Fetch error: \(error) description: \(error.userInfo)")
            }


            guard self.asyncPersonEntityArray != nil else {return}


            //here you can do all you want as updating tableview, make som check, etc:


            for personFound in self.asyncPersonEntityArray {

                let personToPrint = personFound as! Person

                guard personToPrint.name != nil else {
                    print("WARNING! : - name was nil!!")
                    return
                }

                print("Name is: \(personToPrint.name!) and age: \(personToPrint.age)")


            }


            print("...Fetched all entitites")


            //here you can do what you need - end :
            //************************************
        }
        //this block makes the fetch Asynch - end :
        //************************************************************************************

        // MARK: - async fetch request 3
        do {
            try managedContext.execute(asyncFetchRequest)
        } catch let error as NSError {
            print("WARNING! - asyncFetchAllEntities: Could not fetch \(error), \(error.userInfo)")
        }

        print("OK! - fetched completed")

        //end of function
    }

0 个答案:

没有答案