Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从核心数据提取时出现的问题_Ios_Database_Xcode_Core Data_Swift3 - Fatal编程技术网

Ios 从核心数据提取时出现的问题

Ios 从核心数据提取时出现的问题,ios,database,xcode,core-data,swift3,Ios,Database,Xcode,Core Data,Swift3,我在斯威夫特第一次面对这个非常奇怪的问题。我在json中有8个值。通过服务,我正在解析json并将这些值保存在coreData中。所有8个值都将被保存。但是当我从表中获取值时。它有时返回4个值或5个或3个值,就像随机返回,但不是全部返回。我甚至没有应用任何谓词。我无法理解这个问题,所以陷入了困境 这就是我在CoreData中保存它的方式: for field in json as? [AnyObject] ?? [] { self.SaveDayViewDetails(mon

我在斯威夫特第一次面对这个非常奇怪的问题。我在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)
var drname:[字符串]=[]
让fetchRequest1:NSFetchRequest=NSFetchRequest(entityName:“Tbl_SchedulerDayView”)//2
做{
让searchResults=try ServiceCalls.getContext().fetch(fetchRequest1)
对于搜索结果中的trans![NSManagedObject]{
追加(trans.value(forKey:“mioauthid”)为!字符串)
}
}抓住{
打印(“请求错误:\(错误)”)
}
打印(“total docs 1=”,drname.count,“doc names=”,drname)
通常会创建一个moc(延迟),并在应用程序的生命周期中,在您的持久性商店协调员中挂起。您的privateMOC看起来将在保存之前(或可能在保存期间)发布。检查它的寿命。另外,由于您可能正在使用多个MOC,请确保遵循CoreData并发规则&保持MOC之间更改同步的已知有效策略

有关管理主运行中心的示例,请参见此

如果你要使用多个moc,也请阅读苹果的文档。

通常会创建一个moc(懒洋洋地)并在应用程序的生命周期中一直挂在你的持久性商店协调器中。您的privateMOC看起来将在保存之前(或可能在保存期间)发布。检查它的寿命。另外,由于您可能正在使用多个MOC,请确保遵循CoreData并发规则&保持MOC之间更改同步的已知有效策略

有关管理主运行中心的示例,请参见此


如果要使用多个MOC,也请阅读上的苹果文档。

但它保存了所有8个值,我正在打印该部分。问题是当我获取它时。您是否尝试打开sql文件以真正验证它们是否被写入,如果后续获取没有完全返回值?不,我没有,我只是通过print语句确认了它。另外,在获取期间您使用了什么moc?我想ServiceCalls.getContext()会返回不同的moc吗?你的两个MOC可能不同步。事实上,我不太擅长这个上下文,你能用代码详细说明一下吗?像hoe一样,我可以同步这两个moc吗?但它保存了所有8个值,我正在打印该部分。问题是当我获取它时。您是否尝试打开sql文件以真正验证它们是否被写入,如果后续获取没有完全返回值?不,我没有,我只是通过print语句确认了它。另外,在获取期间您使用了什么moc?我想ServiceCalls.getContext()会返回不同的moc吗?你的两个MOC可能不同步。事实上,我不太擅长这个上下文,你能用代码详细说明一下吗?我可以同步这两个moc吗?