【问题标题】:Issues while fetching from Core Data从 Core Data 获取时的问题
【发布时间】:2018-03-06 09:10:45
【问题描述】:

我第一次在 swift 中遇到这个非常奇怪的问题。我在 json 中有 8 个值。通过服务,我正在解析该 json 并将这些值保存在 coreData 中。所有 8 个值都被保存。但是当我从该表中获取值时。它有时会返回 4 个值或 5 或 3 个值,例如随机但不是全部。我什至没有应用任何谓词。我被严重卡住了,因为我无法理解这个问题。

这就是我在 CoreData 中保存它的方式:

    for field in json as? [AnyObject] ?? [] {

     self.SaveDayViewDetails(monthlyid: (field["MonthlyID"] as! String), mioauthid: (field["MIOAuthID"] as! String), empid: (field["EmpID"] as! String), doctorid: (field["DoctorID"] as! String))

  }

class func SaveDayViewDetails (monthlyid: String, mioauthid: String, empid : String,doctorid : String,doccode : String,callplanneractivityid : String,actid : String,startdate : String,speciality : String,s3 : String,s2 : String,s1 : String,plannerid : String,p4 : String,p3 : String,p2 : String,p1 : String,mstatus : String,miostatusreason : String,miostatus : String,miodescription : String,isexecuted : String,g1 : String,end_date : String,doctorname : String,classname : String,address : String,actname : String,actfcolor : String,actbcolor : String,editable : String,planmonth : String,date:String) {
        let context = getContext()

        let privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
        privateMOC.parent = context
        //retrieve the entity that we just created
        let entity =  NSEntityDescription.entity(forEntityName: "Tbl_SchedularDayView", in: context)
        let transc = NSManagedObject(entity: entity!, insertInto: context)
        //set the entity values
        transc.setValue(Int32(monthlyid), forKey: "monthlyid")
        transc.setValue(mioauthid, forKey: "mioauthid")
        transc.setValue(Int32(empid), forKey: "empid")
        transc.setValue(Int32(doctorid), forKey: "doctorid")

        //save the object
        do {
            try privateMOC.save()
            context.performAndWait {
                do {
                    try context.save()
                    print("total saved Tbl_SchedularDayView in Database!")
                } catch {
                    fatalError("Failure to save context: \(error)")
                }
            }


        } catch let error as NSError  {
            print("Could not save \(error), \(error.userInfo)")
        } catch {

            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }
    }

这就是我获取它的方式:

     var drname:[String] = []
   let fetchRequest1:NSFetchRequest<NSFetchRequestResult>  = NSFetchRequest(entityName: "Tbl_SchedularDayView") // 2nd
      do{
              let searchResults = try ServiceCalls.getContext().fetch(fetchRequest1)

               for trans in searchResults as! [NSManagedObject] {
                         drname.append(trans.value(forKey: "mioauthid") as! String)
                 }
             }catch {
               print("Error with request: \(error)")
             }

 print("total docs 1 = ",drname.count," doc names = ",drname)

【问题讨论】:

    标签: ios database xcode core-data swift3


    【解决方案1】:

    通常会在您的持久存储协调器中创建一个 moc(懒惰地)并在应用程序的整个生命周期内挂起。您的 privateMOC 看起来会在保存之前(或可能在保存期间)发布。检查它的寿命。此外,由于您可能正在使用多个 MOC,请务必遵循 CoreData 并发规则和已知的有效策略,以使 MOC 之间的更改保持同步。

    请参阅此tutorial,了解管理 moc 的示例。

    如果您要使用多个 moc,还请阅读 Apple 在 CoreData concurrency 上的文档。

    【讨论】:

    • 但它保存了所有 8 个值,我正在打印那部分。问题是当我拿它的时候。
    • 您是否尝试打开 sql 文件以真正验证它们是否已写入,以防后续提取未完全返回值?
    • 不,我没有,我只是通过打印声明确认了。
    • 另外,您在获取过程中使用的是什么 moc? ServiceCalls.getContext() 返回一个不同的 moc,我猜?你的两个 moc 可能不同步。
    • 其实我不是很擅长这个上下文,你能用代码详细说明一下吗?像锄头一样,我可以同步这两个 moc 吗?
    猜你喜欢
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多