我收到一个JSon,我将其转换为字典数组,并将此数组插入到核心数据中。
此数组的结果有8.000个项目。
如何在核心数据使用以下代码时插入UIProgressView?
谢谢!
func CreateContext() -> NSManagedObjectContext {
let AppDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let Context: NSManagedObjectContext = AppDel.managedObjectContext
return Context
}
static func AddData(arrayDictionary: [[String : AnyObject]]) {
for Item in arrayDictionary {
let Context = CreateContext()
let DAO = NSEntityDescription.insertNewObjectForEntityForName("Customers", inManagedObjectContext: Context)
// Insert at Core Data
if let item = Item["UserID"] as? Int {
DAO.setValue(item, forKey: "userID")
}
if let item = Item["Name"] as? String {
DAO.setValue(item, forKey: "name")
}
if let item = Item["Email"] as? String {
DAO.setValue(item, forKey: "email")
}
do {
try Contexto.save()
} catch {
let nserror = error as NSError
//abort()
}
}
}
}
编辑
自定义类正在插入数据。我试图创建一个协议和委派,但我不知道错误在哪里(真诚地说,我不知道如何使用协议和委派。我试图遵循如何从swift中的下载委派更新gui(progressview)中的示例)
我的ViewController类:
import UIKit
protocol ProgressBarDelegate: class {
func UpdateProgressBar(progress: Float)
}
class MyViewController: UIViewController {
@IBOutlet var progressView: UIProgressView!
func AddDataFromArray() {
let DB = MyCustomClass()
DB.delegate?.UpdateProgressBar(progressView.progress)
DB.AddData(getArrayDictionaryData())
}
}
我的自定义类:
class MyCustomClass {
var delegate: ProgressBarDelegate?
func initWithDelegate(delegate: ProgressBarDelegate) {
self.delegate = delegate
}
func CreateContext() -> NSManagedObjectContext {
let AppDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let Context: NSManagedObjectContext = AppDel.managedObjectContext
return Context
}
func AddData(arrayDictionary: [[String : AnyObject]]) {
var addedItems: Int = 0
var Progress = Float()
for Item in arrayDictionary {
addedItems += 1
Progress = ((Float(100.0) / Float(arrayDictionary.count)) * Float(addedItems)) / Float(100)
let Context = CreateContext()
let DAO = NSEntityDescription.insertNewObjectForEntityForName("Customers", inManagedObjectContext: Context)
// Insert at Core Data
if let item = Item["UserID"] as? Int {
DAO.setValue(item, forKey: "userID")
}
if let item = Item["Name"] as? String {
DAO.setValue(item, forKey: "name")
}
if let item = Item["Email"] as? String {
DAO.setValue(item, forKey: "email")
}
do {
try Contexto.save()
} catch {
let nserror = error as NSError
//abort()
}
delegate!.UpdateProgressBar(Progress)
}
}
}
您似乎有点误解了这些关系。
如果一个类需要提供一些信息,或者想进行回调以让某人知道发生了什么,那么它可以接受委托。委托实现委托协议中指定的方法并处理通知:
Delegate (observer)
-> conforms to the delegate protocol
-> registers itself
-> handles notifications
Interesting class
-> holds a reference to a delegate
-> notifies the delegate when things happen
就你而言。MyViewController
应该符合委托协议,所以它应该实现UpdateProgressBar
。它创建一个MyCustomClass
的实例来为它做一些工作,并将自己设置为委托。然后MyCustomClass
做一些工作并调用UpdateProgressBar
,然后MyViewController
更新UI。
由于从未设置代理,您现在会崩溃。此行:
DB.delegate?.UpdateProgressBar(progressView.progress)
应该是
DB.delegate = self
旁白:
如果某个东西实际上并没有创建,就不要将其称为CreateContext
。此外,函数的第一个字母应该是小写字母。
假设进度视图已经存在,您可以执行以下操作:
self.progressView.progress = 0
for (index, item) in arrayDictionary.enumerate() {
// Do all the work of importing the data...
let progress = CGFloat(index) / CGFloat(arrayDictionary.count)
self.progressView.progress = progress
}
随着循环的不断执行,progress
的值将逐渐从接近零上升到1.0。