title: over writing mangedObjectContext - data dissapearing
body: <p>The setup is this:</p>
  
  <ul>
  <li>I have  a table view that populates with two different sets of data from two different methods located in the same class.</li>
  <li>When I run the first set of data by itself, it works great.  </li>
  <li>When I instantiate the second method that is suppose to grab data based on an ID within the first set of data which is pulled with each loop of the "cellForRowAtIndexPath" , it breaks as if I tried to call on data that no longer exists.</li>
  </ul>
  
  <p>If feels like somewhere in the process the first data set is being replaced by the second data set.</p>
  
  <p>So, the below works.</p>
  
  <pre><code>import Foundation
  import UIKit
  import CoreData
  
  class LendersViewController: UIViewController {
  
  var lenders = Array&lt;Lenders&gt;()
  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) -&gt; Int {
      //return lenderData.count!
      return self.lenders.count
  }
  
  
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; 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!
  }
  </code></pre>
  
  <p>But as soon as I modify the "cellForRowAtIndexPath" with the below, which calls on the second method for its data set, into cellForRowAtIndexPath ...</p>
  
  <pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; 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
  }
  </code></pre>
  
  <p>The first iteration works but the second iteration throws this error.</p>
  
  <pre><code>"fatal error: unexpectedly found nil while unwrapping an Optional value"
  </code></pre>
  
  <p>The error here isn't the problem though.  I get that the value "getLenderId" is nil and unwrapping it as nil is throwing the error.</p>
  
  <p>The problem is that the data set from the first method disappears after I instantiate the second method.</p>
  
  <p>Couple points.</p>
  
  <ol>
  <li>Both methods are in the same class but read data from two different Core data tables.</li>
  <li>I am using NSFetchedResultsControllerDelegate in that class and that is how the data is manages prior to sending it as an array to the processes making the request.</li>
  </ol>
  
  <p>I searched the site but came across lots of unrelated topics.  I am sure that someone has run into this before so if this has been asked with greater clarity a link to the better question would be appreciated.</p>
  
  <p>Thanks all,</p>
  
  <p>p.s. Here is the model class with the methods that are being called.</p>
  
  <pre><code>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()-&gt; Array&lt;Lenders&gt;{
      // 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&lt;Lenders&gt;
  }
  
  
  // Reads User's comments on given data.
  func readUserCommentsData(lenderId:String)-&gt; Array&lt;LenderUserComments&gt;{
  
      // 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&lt;LenderUserComments&gt;
  }
  
  ....
  
  }
  </code></pre>
  
  <p>Added at the request of the a commenter.</p>
  
  <pre><code>    var managedObjectContext: NSManagedObjectContext {
  
          let coordinator = self.persistentStoreCoordinator
  
              _managedObjectContext = NSManagedObjectContext()
              _managedObjectContext!.persistentStoreCoordinator = coordinator
  
  
      return _managedObjectContext!
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Solution</strong>  :I replaced the managedObjectContext variable in AppDelegate.swift.  Credits to Hashmat Khalil for providing the core of the solution.  The below works like a charm.</p>
  
  <pre><code>    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!
  }
  </code></pre>