设置如下:
如果感觉到过程中的某个地方,第一个数据集将被第二个数据集替换。
所以,下面的作品。
import Foundation
import UIKit
import CoreData
class LendersViewController: UIViewController {
var lenders = Array<Lenders>()
let lendersModel = LendersModel()
//MARK: VC Delegate Methods
override func viewDidLoad() {
// Get the data from CoreData and Put it into a variable that I can get to later.
self.lenders = lendersModel.readLenderData() as Array
}
}
extension LendersViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return lenderData.count!
return self.lenders.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
// Set the cell to reference the LendersViewCell class below.
// This contains all the connections to the cell.
let cell: LendersViewCell = tableView.dequeueReusableCellWithIdentifier("LenderCell") as LendersViewCell
let lendersNSArray = lenders as NSArray
cell.cellLenderName.text = lendersNSArray[indexPath.row].valueForKey("corpName") as String?
return cell
}
}
class LendersViewCell: UITableViewCell {
@IBOutlet weak var cellLenderLogo: UIImageView!
@IBOutlet weak var cellAprValue: UILabel!
@IBOutlet weak var cellDownValue: UILabel!
@IBOutlet weak var cellMonthlyValue: UILabel!
@IBOutlet weak var cellFavButtonLabel: UIButton!
@IBOutlet weak var cellLenderName: UILabel!
}
但是一旦我修改了下面的“cellForRowAtIndexPath”,它调用了其数据集的第二个方法,就进入了cellForRowAtIndexPath ......
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
// Set the cell to reference the LendersViewCell class below.
// This contains all the connections to the cell.
let cell: LendersViewCell = tableView.dequeueReusableCellWithIdentifier("LenderCell") as LendersViewCell
let lendersNSArray = lenders as NSArray
cell.cellLenderName.text = lendersNSArray[indexPath.row].valueForKey("corpName") as String?
//Adding the below two lines causes an error.
var getLenderId = lendersNSArray[indexPath.row].valueForKey("lenderId") as? String
self.lenderUserComments = lendersModel.readUserCommentsData(getLenderId!)
return cell
}
第一次迭代有效,但第二次迭代会抛出此错误。
"fatal error: unexpectedly found nil while unwrapping an Optional value"
这里的错误不是问题所在。我得到值“getLenderId”为nil并将其展开为nil抛出错误。
问题是在我实例化第二种方法后第一种方法的数据集消失了。
情侣点。
我搜索了该网站但发现了许多不相关的主题。我确信有人之前遇到过这种情况,所以如果更清楚地问这个问题,那么可以理解与更好问题的联系。
全心全意,
P.S。这是带有被调用方法的模型类。
import Foundation
import CoreData
import UIKit
class LendersModel:NSObject, NSFetchedResultsControllerDelegate{
//MARK: - Variables
// *** Setup the params used to work with the database.
private let entityName:String = "LenderUserComments"
private let entityName2:String = "Lenders"
private let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
//Reads ALL lender data
func readLenderData()-> Array<Lenders>{
// Setup database connection and save to context.
var context:NSManagedObjectContext = appDel.managedObjectContext
var request = NSFetchRequest(entityName: entityName2)
request.returnsObjectsAsFaults = false;
// Get the data and put it into a variable
var results = context.executeFetchRequest(request, error: nil)
return results as Array<Lenders>
}
// Reads User's comments on given data.
func readUserCommentsData(lenderId:String)-> Array<LenderUserComments>{
// Setup database connection and save to context.
var context:NSManagedObjectContext = appDel.managedObjectContext
var request = NSFetchRequest(entityName: entityName)
request.returnsObjectsAsFaults = false;
request.predicate = NSPredicate(format: "lenderId = %@", lenderId)
// Get the data and put it into a variable
var results = context.executeFetchRequest(request, error: nil)
return results as Array<LenderUserComments>
}
....
}
根据评论者的要求添加。
var managedObjectContext: NSManagedObjectContext {
let coordinator = self.persistentStoreCoordinator
_managedObjectContext = NSManagedObjectContext()
_managedObjectContext!.persistentStoreCoordinator = coordinator
return _managedObjectContext!
}
解决方案:我替换了AppDelegate.swift中的managedObjectContext变量。致Hashmat Khalil提供解决方案的核心。下面的作品就像一个魅力。
var managedObjectContext: NSManagedObjectContext {
if (_managedObjectContext != nil)
{
return _managedObjectContext!; // making sure you don't reinitialise
}
let coordinator = self.persistentStoreCoordinator
_managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
_managedObjectContext!.persistentStoreCoordinator = coordinator
return _managedObjectContext!
}
答案 0 :(得分:1)
所以,现在每次访问appDel.managedObjectContext
时都要查看代码,以便创建新实例。当旧实例消失时,您也无法访问数据,您必须重新获取所有内容。所以请确保您只使用一个实例。并且GUI上下文最好具有主要的并发性。
var managedObjectContext: NSManagedObjectContext {
if (_managedObjectContext)
{
return _managedObjectContext; // making sure you don't reinitialise
}
let coordinator = self.persistentStoreCoordinator
_managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)
_managedObjectContext!.persistentStoreCoordinator = coordinator
return _managedObjectContext!
}
是的。