【问题标题】:Coredata not giving data on fetch requestCoredata 未提供有关 fetch 请求的数据
【发布时间】:2016-07-18 07:46:10
【问题描述】:

在我的应用程序中,当我启动我的应用程序并从 coredata 获取数据时。它没有给我数据。

但是如果我没有关闭我的应用程序并将一些数据保存在数据库中,并且如果我获取数据而不关闭我的应用程序,它会给我一个数据。

当我关闭我的应用程序时。下次又不给我数据了。

这是我的代码

self.FetchAllLocalData({ (Available) in
                if (Available == "0")
                {
                    for ValueToSave in detailArray!
                    {
                        let entity = NSEntityDescription.insertNewObjectForEntityForName("RxOrder", inManagedObjectContext: moc)
                        print(ValueToSave.id!)

                        var medicinetype : String = ""
                        let Id : String = ValueToSave.id!.description
                        let isRx : String = ValueToSave.isRxMedicine!.description
                        print(ValueToSave.medicineTypeId)
                        if (ValueToSave.medicineTypeId != nil)
                        {
                            medicinetype = ValueToSave.medicineTypeId!
                        }
                        else
                        {
                            medicinetype = "0"
                        }

                        let medicineName : String = ValueToSave.name!
                        let orderId : String = ValueToSave.orderId!.description
                        let price : String = "0"
                        let quantity : String = ValueToSave.quentity!.description
                        let strength : String = ValueToSave.strength!


                        entity.setValue(Id, forKey: "medicineId")
                        entity.setValue(isRx, forKey: "isRxMedicine")
                        entity.setValue(medicinetype, forKey: "medicineType")
                        entity.setValue(medicineName, forKey: "productName")
                        entity.setValue(self.order_id_RX_Medicine!, forKey: "OrderId")
                        entity.setValue(price, forKey: "price")
                        entity.setValue(quantity, forKey: "quantity")
                        entity.setValue(strength, forKey: "strength")


                        do{
                            try moc.save()
                        }
                        catch {
                            fatalError("failed To Save Content\(error)")
                        }


                    }

还有这个用于获取

func  FetchAllLocalData(completion : (Available : String)-> Void)  {
     let request : NSFetchRequest = NSFetchRequest(entityName: "RxOrder")


    do{


        //request.predicate = NSPredicate(format: "orderId == \(order_id_RX_Medicine!)")
        let fetchedPerson = try moc.executeFetchRequest(request)
        DataAvailable = fetchedPerson as! [NSManagedObject]
        print(DataAvailable.count)

        if (DataAvailable.count > 0)
        {
            print(DataAvailable[0].valueForKey("orderId"))
            for OldData in DataAvailable {
                print(DataAvailable.count)

                print(order_id_RX_Medicine)
                print(OldData.valueForKey("orderId"))
                if (OldData.valueForKey("orderId")! as! String == order_id_RX_Medicine)
                {
                   completion(Available: "1")
                }
                else
                {
                    completion(Available: "0")
                }

            }

        }
        else
        {
           completion(Available: "0")
        }

    }

    catch
    {
        completion(Available: "0")
        fatalError("Something Went Wrong \(error)")
    }


}

【问题讨论】:

    标签: ios swift core-data


    【解决方案1】:

    首先,由于您在这里并且代码中有print 语句,我假设您看到了这些语句,并且在您的应用程序运行时您没有收到错误。

    因此,查看您的 Core Data 创建代码会很有帮助。

    了解如何将moc 放入创建代码中也会很有帮助。创建代码是在 AppDelegate 还是其他地方?

    您使用的是什么类型的持久存储?

    【讨论】:

      【解决方案2】:

      您必须在您的应用委托中尝试此操作:

          func applicationWillTerminate(application: UIApplication) {
          // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
          AppDelegate.shared.saveContext()
      }
      

      这将在应用终止之前保存您的数据

      【讨论】:

        猜你喜欢
        • 2019-09-11
        • 1970-01-01
        • 2017-05-21
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 2018-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多