0
  1. RootVC.swift

    private let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    
    private var fetchRecordRequestController: NSFetchedResultsController = NSFetchedResultsController()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
    // Initialize fetch request
    fetchRecordRequestController = NSFetchedResultsController(fetchRequest: fetchRecordRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
    fetchRecordRequestController.delegate = self
    // Fetch data from object
    do {
        try fetchRecordRequestController.performFetch()
    } catch let fetchError as NSError {
        print("(RootVC)fetchError: \(fetchError), \(fetchError.userInfo)")
    }}
    
  2. AddRuleVC.swift

// Initialize managedObjectContext for creating a new object from the input

private var managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

// Method to save the extracted values to CoreData Objects

func saveValues() {

// Setting up, to save on a private queue

let privateManagedObjectContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateManagedObjectContext.persistentStoreCoordinator = managedObjectContext.persistentStoreCoordinator

// Initialize entity description

let RuleDescription = NSEntityDescription.entityForName("Rule",inManagedObjectContext:privateManagedObjectContext)
let rule = Rule(entity: RuleDescription!, insertIntoManagedObjectContext: privateManagedObjectContext)

// Perform Block

privateManagedObjectContext.performBlock { () -> Void in

// Set object values from temporary variables

rule.music = musicTemp! as MPMediaItemCollection
    print("musicTemp = \(musicTemp!.count)=========>saveValues")
// Save the object

    do {

    //print(NSStringFromClass((self.rule?.classForCoder)!))

            try rule.managedObjectContext?.save()
        } catch let saveError as NSError {
            print("(AddRuleViewController)saveError: \(saveError), \(saveError.userInfo)")
        }
    }
}

My RootVC is for fetching the data stored using AddRuleVC. I can save as many rules as I'd want to, but they do not show once the AddRuleVC segues back to the RootVC. They only show up after a relaunch.

Varun Kumar
  • 344
  • 2
  • 24
Shyam
  • 561
  • 5
  • 22
  • How are you attempting to save this `MPMediaItemCollection`? That class does not conform to `NSCoding`, so what are you doing to try and put it into Core Data? – Tom Harrington Aug 30 '16 at 16:06
  • As I have an attribute, which is "transformable", I pass `MPMediaItemCollection` to it and save it. It works perfectly, and there are no errors while saving. I can again retrieve the same by typecasting it as `MPMediaItemCollection`, and am able to use it for queuing it up or retrieving metadata for each track by passing it into array of `[MediaItem]`s. – Shyam Aug 30 '16 at 16:39
  • @TomHarrington, any idea about the error, `unrecognized selector sent to instance` – Shyam Aug 31 '16 at 15:43
  • Most likely you're using something that's not a collection (probably an array) where a collection is expected. Can't say more without more details of the code. – Tom Harrington Aug 31 '16 at 16:29
  • @TomHarrington, I've added more detail. Please let me know if I'm missing anything. Sorry, for probably asking a very trivial question. New to swift!! :-| – Shyam Aug 31 '16 at 16:56
  • Sorry, I really can't tell. – Tom Harrington Aug 31 '16 at 22:50
  • No worries, @TomHarrington. Thanks for trying, though. – Shyam Aug 31 '16 at 23:51
  • By the way, just confirmed that `MPMediaItemCollection`, conforms to `NSSecureCoding`. Not sure, if this will help you @TomHarrington, in providing me a way out of the quicksand, that I am into for the past week. – Shyam Sep 01 '16 at 01:05
  • @TomHarrington, I've modified the information, as I'm facing another problem. And, thanks again, for your help in the earlier issue. – Shyam Sep 01 '16 at 15:13

0 Answers0