Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Swift 在iOS9中尝试从数据库中获取超过512项时,该方法挂起。但它适用于iOS10及更高版本_Swift_Sqlite_Cocoapods_Ios9_Fetch - Fatal编程技术网

Swift 在iOS9中尝试从数据库中获取超过512项时,该方法挂起。但它适用于iOS10及更高版本

Swift 在iOS9中尝试从数据库中获取超过512项时,该方法挂起。但它适用于iOS10及更高版本,swift,sqlite,cocoapods,ios9,fetch,Swift,Sqlite,Cocoapods,Ios9,Fetch,我有一种从数据库中提取数据的方法,该方法适用于所有版本的iOS 10及以上版本,但对于iOS 9,如果我尝试获取超过512项,则该方法在调用fetch后挂起,代码如下: internal func getAll(_ dictId:Int) -> [Word]? { let dictFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Words") dictFetch.predicate = NSPr

我有一种从数据库中提取数据的方法,该方法适用于所有版本的iOS 10及以上版本,但对于iOS 9,如果我尝试获取超过512项,则该方法在调用fetch后挂起,代码如下:

internal func getAll(_ dictId:Int) -> [Word]? {
    let dictFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Words")
    dictFetch.predicate = NSPredicate(format: "dictionary = %@", dictId)
    dictFetch.fetchLimit = 513
        do {
            print("getAll before fetching")
            let res = try self.moc_.fetch(dictFetch) as? [Word]
            print("getAll after fetching")
            return res
        } catch {
            print("error: \(error)")
        }
    return nil
}
但如果我将fetchLimit设置为513或更多,则此代码将显示以下日志:

getAll before fetching
没有任何错误,只是挂起

为了进行测试,我只在AppDelegate中调用了这个方法(项目中没有任何其他方法)。同样的结果。我还尝试异步调用它。一样。 我还更改了“dictionary”和排序的输入值,以检查这是否是由于表中的某个元素造成的。同样,不超过512个元素。 表“Word”包含6000多个项目,在iOS 10及更高版本中一切正常。它挂起时也没有“fetchLimit”。
请给我任何建议。

我找到了解决问题的办法。对于ios 9及以下版本的设备,创建一个循环,从数据库中获取500项,直到获取所有必要的数据。 也许有人会派上用场

    // CHECK VERSION OF IOS
let fetchLimit = NSString(string: UIDevice.current.systemVersion).doubleValue >= 10 ? -1 : 500

internal func getAll(_ dictId:Int) -> [Word]? {
    return getAll(dict, nil, nil)
}

internal func getAll(_ dict:Dictionary,_ aRes:[Word]?,_ lastReg:NSDate?) -> [Word]? {
    let dictFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Words")
    if lastReg != nil {
        dictFetch.predicate = NSPredicate(format: "dictionary = %@ AND registration > %@", dict, lastReg!)
    } else {
        dictFetch.predicate = NSPredicate(format: "dictionary = %@", dictId)
    }
    let sort = NSSortDescriptor(key: "registration", ascending: true)
    dictFetch.sortDescriptors = [sort]
    if fetchLimit > 0 {
        dictFetch.fetchLimit = fetchLimit
    }

    do {
        if let res = try self.moc_.fetch(dictFetch) as? [Word] {
            var aRes_ = aRes != nil ? aRes! + res : res
            if res.count == fetchLimit {
                return self.getAll(dict, aRes_, aRes_[aRes_.count - 1].registration)
            }
            return aRes_
        }
        return aRes
    } catch {
        Titles.l(self, "getAll_, error:  \(error)")
    }
    return nil
}
//检查IOS的版本
让fetchLimit=NSString(字符串:UIDevice.current.systemVersion)。doubleValue>=10-1 : 500
内部函数getAll(dictd:Int)->[Word]?{
返回getAll(dict,nil,nil)
}
内部函数getAll(udict:Dictionary,aRes:[Word]?,uulastreg:NSDate?)->[Word]?{
let dictFetch=NSFetchRequest(entityName:“Words”)
如果lastReg!=nil{
dictFetch.predicate=NSPredicate(格式:“dictionary=%@和registration>%@”,dict,lastReg!)
}否则{
dictFetch.predicate=NSPredicate(格式:“dictionary=%@”,dictd)
}
让sort=NSSortDescriptor(键:“注册”,升序:true)
dictFetch.sortDescriptors=[排序]
如果fetchLimit>0{
dictFetch.fetchLimit=fetchLimit
}
做{
如果let res=try self.moc_u.fetch(dictFetch)as?[Word]{
战神战神!=无?战神!+res:res
如果res.count==fetchLimit{
return self.getAll(dict,战神,战神[aRes.count-1]。注册)
}
返回战神_
}
返回战神
}抓住{
Titles.l(self,“getAll,error:\(error)”)
}
归零
}
    // CHECK VERSION OF IOS
let fetchLimit = NSString(string: UIDevice.current.systemVersion).doubleValue >= 10 ? -1 : 500

internal func getAll(_ dictId:Int) -> [Word]? {
    return getAll(dict, nil, nil)
}

internal func getAll(_ dict:Dictionary,_ aRes:[Word]?,_ lastReg:NSDate?) -> [Word]? {
    let dictFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Words")
    if lastReg != nil {
        dictFetch.predicate = NSPredicate(format: "dictionary = %@ AND registration > %@", dict, lastReg!)
    } else {
        dictFetch.predicate = NSPredicate(format: "dictionary = %@", dictId)
    }
    let sort = NSSortDescriptor(key: "registration", ascending: true)
    dictFetch.sortDescriptors = [sort]
    if fetchLimit > 0 {
        dictFetch.fetchLimit = fetchLimit
    }

    do {
        if let res = try self.moc_.fetch(dictFetch) as? [Word] {
            var aRes_ = aRes != nil ? aRes! + res : res
            if res.count == fetchLimit {
                return self.getAll(dict, aRes_, aRes_[aRes_.count - 1].registration)
            }
            return aRes_
        }
        return aRes
    } catch {
        Titles.l(self, "getAll_, error:  \(error)")
    }
    return nil
}