Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift loop for...in core data

I need to loop into a core data to compare two values, I try to do it with a for...in.

I'm using fetchedResultController to create my request on my Core Data:

func getFetchedResultsControllerUser() -> NSFetchedResultsController{
    frcu = NSFetchedResultsController(fetchRequest: taskFetchRequestUser(), managedObjectContext: context!, sectionNameKeyPath: nil, cacheName: nil)
    return frcu
}

func taskFetchRequestUser() -> NSFetchRequest {
    let fetchRequest = NSFetchRequest(entityName: "User")
    let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest
}

and on my cellForRowAtIndexPath I declare my user object: let user = frcu.objectAtIndexPath(indexPath) as! User

This is the user class:

import Foundation
import CoreData

@objc(User)
class User: NSManagedObject {

    @NSManaged var id: String
    @NSManaged var name: String
    @NSManaged var task: NSSet

}

Once I'm there, I try to loop on user to get a specified id:

for userId in user.id{
      if (userId == task.responsable){
            responsible = user.name
        }      
}

task is let task = frc.objectAtIndexPath(indexPath) as! Task, I can add code about this if needed.

but it doesn't work, I have the error Could not find an overload for "==" that accepts the supplied arguments line if (userId == task.responsable)

Am I on the good way, what am I doing bad?

I just try to compare task.responsable with every user.id and if I get a matching value, I print the corresponding user.name.. It's a very common thing with a traditional ORM, but there I'm stuck...

ps: My final aim is:

Compare the task.responsable string with every user.id and if I get a matching value, print the corresponding user.name of the user.id

This is the Task Entity and it's frc:

import Foundation
import CoreData

@objc(Task)
class Task: NSManagedObject {

    @NSManaged var context: String
    @NSManaged var date: String
    @NSManaged var detail: String
    @NSManaged var folder: String
    @NSManaged var id: String
    @NSManaged var responsable: String
    @NSManaged var status: String
    @NSManaged var summary: String
    @NSManaged var user: User
}

var frc : NSFetchedResultsController = NSFetchedResultsController()

func getFetchedResultsController(String) -> NSFetchedResultsController{
    frc = NSFetchedResultsController(fetchRequest: taskFetchRequest(folder), managedObjectContext: context!, sectionNameKeyPath: nil, cacheName: nil)
    return frc
}

func taskFetchRequest(String) -> NSFetchRequest {
    let fetchRequest = NSFetchRequest(entityName: "Task")
    let predicate = NSPredicate(format: "folder = %@", folder)
    let sortDescriptor = NSSortDescriptor(key: "summary", ascending: true)
    fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest
}
like image 588
fselva Avatar asked Jul 15 '15 08:07

fselva


1 Answers

The problem lies in your for-in loop. user (lower case u) is a single User object - so the for-in loop is not iterating through all the User objects. Because user.id is a String, the loop is iterating through each character of that string in turn.

To iterate through all of the User objects, use the fetched results controller's fetchedObjects.

for user in frcu.fetchedObjects! as! [User] {
    if (user.id == task.responsable){
        responsible = user.name
    }      
}
like image 151
pbasdf Avatar answered Oct 12 '22 23:10

pbasdf