2014-11-30 3 views
0

이 DB 구조는 내 CoreData에 있습니다. 모든 Task 개체를 동일한 Group 개체를 가진 Task 개체에 가져와야합니다 (즉, 그룹에 내가 원하는 이러한 작업이 포함되어 있습니다). 내가 어떻게 Predicates 또는 그 밖의 무엇인가 할 수 있냐? enter image description here동일한 객체가있는 엔티티에서 객체 목록을 가져 오는 방법은 무엇입니까? - ios

나는이 코드를 시도 :

var fetchedResultsController: NSFetchedResultsController { 
    if _fetchedResultsController != nil { 
     return _fetchedResultsController! 
    } 

    let fetchRequest = NSFetchRequest() 
    // Edit the entity name as appropriate. 
    let entity = NSEntityDescription.entityForName("Task", inManagedObjectContext: self.managedObjectContext!) 
    fetchRequest.entity = entity 

    // Set the batch size to a suitable number. 

    // Edit the sort key as appropriate. 
    let sortDescriptor = NSSortDescriptor(key: "createdate", ascending: false) 
    let sortDescriptors = [sortDescriptor] 

    fetchRequest.sortDescriptors = [sortDescriptor] 
    var predicate = NSPredicate(format: "group.createdate == %@", group!.createdate) 
    fetchRequest.predicate = predicate 

    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Details") 
    aFetchedResultsController.delegate = self 
    _fetchedResultsController = aFetchedResultsController 

    var error: NSError? = nil 
    if !_fetchedResultsController!.performFetch(&error) { 
     // Replace this implementation with code to handle the error appropriately. 
     // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
     //println("Unresolved error \(error), \(error.userInfo)") 
     abort() 
    } 

    return _fetchedResultsController! 
} 

답변

1

먼저, 당신이 기준으로 필터링 할 그룹을 얻을 수 (나는 그것이 코드에서 "group"라고 생각합니다). 유효한 개체인지 확인하거나 풀어 놓은 후 크래시가 발생하면 !으로 처리됩니다. 다음과 같이 가져온 결과 컨트롤러에 조건자를 추가하십시오.

fetchRequest.predicate = NSPredicate(format:"group = %@", group) 

그게 전부입니다 ;-).

+0

@ Fogmeister - 그의 FRC에서는 작동하지 않습니다. – Mundi

+0

아 그래. 그것이 frc를위한 것인지 몰랐습니다. – Fogmeister

+0

Daaamn. 나는 그러나 "그룹 == % @"시도했다. 고마워요 !! –

관련 문제