Coredata未提供撷取要求的资料 [英] Coredata not giving data on fetch request
问题描述
在我的应用程序,而我启动我的应用程序和我从coredata提取数据。它不给我数据。
但如果我没有关闭我的应用程序并保存一些数据在数据库中,没有关闭我的应用程序,如果我获取数据,它给我一个数据。 / p>
和我关闭我的应用程序。
以下是我的代码
self.FetchAllLocalData(
中的{可用)if(可用==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。名称!
let orderId:String = ValueToSave.orderId!.description
let price:String =0
let quantity:String = ValueToSave.quentity!。Desccription
let strength:String = ValueToSave.strength!
entity.setValue(Id,forKey:medicineId)
entity.setValue(isRx,forKey:isRxMedicine)
entity.setValue forKey:medicineType)
entity.setValue(medicineName,forKey:productName)
entity.setValue(self.order_id_RX_Medicine !, forKey:OrderId)
entity.setValue ,forKey:price)
entity.setValue(quantity,forKey:quantity)
entity.setValue(strength,forKey:strength)
$ b b do {
try moc.save()
}
catch {
fatalError(无法保存Content \(错误))
}
}
$ b
func FetchAllLocalData(completion:(Available:String) - > Void){
let request:NSFetchRequest = NSFetchRequest(entityName:RxOrder)
do {
//请求。 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(可用:1)
}
else
{
完成(可用:0)
}
}
}
else
{
完成(可用:0)
}
}
catch
{
fatalError(Something Went Wrong \(error))
}
}
首先,因为你在这里,并且有 print
代码我将假设你看到这些火,你的应用程序运行时没有得到错误。
因此,这将有助于看到你的核心数据创建代码。
它也将有助于看到你如何得到 moc
到创建代码。是在AppDelegate还是其他地方的创建代码?
您使用什么类型的持久存储?
in my app while i start my app and i fetch data from coredata. its not giving me data.
but if i not closed my app and save some data in database and without close my app if i fetch data its giving me a data.
and when i close my app. next time again its not giving me data.
Here is my code
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)")
}
}
and this one for fetching
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)")
}
}
First, since you are here and there are print
statements in your code I am going to assume you are seeing those fire and you are not getting an error when your application runs.
Therefore, it would be helpful to see your Core Data creation code.
It would also be helpful to see how you get the moc
into the creation code. Is the creation code in the AppDelegate or somewhere else?
What type of persistent store are you using?
这篇关于Coredata未提供撷取要求的资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!