-1

I am working on iPhone application where I need to implement one to many relationship from one entity to another entity.I have export NSManagedObject subclass from coredata database but could not find relation accessors in core data model file like Objective-C.

Although I'm able to set data in relationship NSSet but this only persist while iPhone app is running. Once I kill and restart the application I didn't get entity relationship for in fetch request from core data.

I am not sure what I am doing wrong. It will be great if anyone can tell me how to set data in NSSet relationship object. Any sample example will be a great help

Here is my core data model files. One folder can content multiple content as folder detail

extension FolderContent {

    @NSManaged var contentID: NSNumber?
    @NSManaged var contentTitle: String?
    @NSManaged var contentType: String?
    @NSManaged var publishDate: String?
    @NSManaged var folderList: NSSet?

}

extension FolderList {

    @NSManaged var folderID: NSNumber?
    @NSManaged var folderName: String?
    @NSManaged var folderDetail: NSSet?

}

func updateFolderList()
{
    // Initialize Fetch Request
    let fetchRequest = NSFetchRequest()

    // Create Entity Description
    let entityDescription = NSEntityDescription.entityForName(FOLDER_LIST, inManagedObjectContext: self.managedObjectContext)

    // Configure Fetch Request
    fetchRequest.entity = entityDescription

    do {
        let result = try self.managedObjectContext.executeFetchRequest(fetchRequest).last as! FolderList

        let content = result.mutableSetValueForKey("folderDetail")
        content.addObject(self.getContent())

          var folderContent:FolderContent = result.folderDetail?.allObjects.first as! FolderContent
        print(folderContent.contentTitle)
        self.save()

        print(result)

    } catch {
        let fetchError = error as NSError
        print(fetchError)
    }

}
    func getContent()->FolderContent
{
    let folderContent = NSEntityDescription.insertNewObjectForEntityForName(FOLDER_CONTENT, inManagedObjectContext: self.managedObjectContext) as! FolderContent
    folderContent.contentID = 1
    folderContent.contentTitle = "Sandeep"
    folderContent.contentType = "Product"
    return folderContent
}
Sandeep Dhama
  • 614
  • 1
  • 6
  • 20
  • Did you meant that you failed to fetch the relationship records only or whole records that you persisted in database? – DavidODW Feb 24 '16 at 15:47
  • i failed to fetch the relationship records only. – Sandeep Dhama Feb 24 '16 at 15:49
  • If that case, there is a problem when you are persisting the records into core data. Kindly refer to [Saving CoreData to-many relationships in Swift](http://stackoverflow.com/questions/25127090/saving-coredata-to-many-relationships-in-swift). Maybe you can get some answers from there. – DavidODW Feb 24 '16 at 15:54
  • I have tried that without success. can you give me any sample how can i update or insert into NSSet relationship object? – Sandeep Dhama Feb 24 '16 at 15:58
  • a `FolderContent` have many `FolderList`; a `FolderList` is belongs to a `FolderContent`. Is that I understand your record's relationship correctly? – DavidODW Feb 24 '16 at 16:00

2 Answers2

2

If the relationship of FolderContent and FolderList is defined as

  • A FolderContent have many FolderList(s)
  • A FolderList only belongs to a FolderContent

FolderContent

enter image description here

extension FolderContent {
    @NSManaged var contentID: NSNumber?
    @NSManaged var contentTitle: String?
    @NSManaged var contentType: String?
    @NSManaged var publishDate: String?
    @NSManaged var folderList: Set<FolderList>?
}

FolderList

enter image description here

extension FolderList {
    @NSManaged var folderID: NSNumber?
    @NSManaged var folderName: String?
    @NSManaged var folderDetail: FolderContent?
}

Let say you want to persist the record and its relationship

func persistRecords() {

    // Insert the new records
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext

    let folderContentEntity = NSEntityDescription.entityForName("FolderContent", inManagedObjectContext: managedContext)
    let folderListEntity = NSEntityDescription.entityForName("FolderList", inManagedObjectContext: managedContext)

    //Create FolderContent record
    let folderContentObject = FolderContent(entity: folderContentEntity!, insertIntoManagedObjectContext: managedContext)
    folderContentObject.setValue(CONTENTID, forKeyPath: "contentID")
    ...

    //Create FolderList record
    let folderListObject = FolderList(entity: folderListEntity!, insertIntoManagedObjectContext: managedContext)
    folderListObject.setValue(FOLDERID, forKeyPath: "folderID")
    ...

    //Set relationship here
    folderListObject.folderDetail = folderContentObject

    do {
      try managedContext.save()
    } catch let error as NSError  {
      print("Could not save \(error), \(error.userInfo)")
    }
  }
DavidODW
  • 417
  • 4
  • 16
  • Thanks David for your full detail.Can you please show me how to update and delete the relationship objects as well? – Sandeep Dhama Feb 24 '16 at 17:36
  • You can learn how to update and delete methods from here https://www.andrewcbancroft.com/2015/02/18/core-data-cheat-sheet-for-swift-ios-developers/ – DavidODW Feb 24 '16 at 17:46
1

I am guessing you did something like folder.folderDetail.addObject. With core data that won't work because core data behind the scenes is doing lots of things to maintain graph integrity. You have to use mutableSetValueForKey to get the set and have core data work its magic.

user965972
  • 2,489
  • 2
  • 23
  • 39
  • I have update my question. You can check how i am setting data in relation.my one question is how result.mutableSetValueForKey("folderDetail") works how core data know "folderDetail" as a mutable key? it it something we need to define somewhere? – Sandeep Dhama Feb 24 '16 at 16:03
  • 1
    You have to define your entities and relationships in the core data model editor. Otherwise it will never work. I think you have to take a step back and read some tutorials from the start on setting up a core data based application. Defining folderDetail in the model would be one of the first things you do. – user965972 Feb 24 '16 at 16:13