0

좋아, 지난 1-2 주 동안 검색을 시도했는데 제대로 작동하지 않았습니다. NSFRC 없이는 원하는 것을 얻을 수 있지만 성능상의 이유로 NSFRC와 함께하고 싶습니다. 그래서, 나는 2 엔티티와 DataModel이있다 - 아주 분명하다 - 사진 to-many RelationshipNSFetchedResultsController 및 다 대다 관계가 작동하지 않습니다.

가 하나 개의 계정은 한 계정이 많은 accountchanges을 가질 수 있습니다를 참조하십시오. 그래서 계정을 선택하고 특정 계정에 대한 모든 AccountChanges를 표시하려고합니다. 지금까지 계정을 가져 와서 cellForRow 함수에서 NSSet에 액세스 할 수 있었지만 올바른 섹션과 numberOfRowsInSection을 얻지 못했습니다. 이것이 주요 문제입니다.

func numberOfSections(in tableView: UITableView) -> Int { 

    print("Sections : \(self.fetchedResultsController.sections?.count)") 
    if (self.fetchedResultsController.sections?.count)! <= 0 { 
     print("There are no objects in the core data - do something else !!!") 
    } 
    return self.fetchedResultsController.sections?.count ?? 0 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print("Section Name") 
    print(self.fetchedResultsController.sections![section].name) 
    let sectionInfo = self.fetchedResultsController.sections![section] 
    print("Section: \(sectionInfo) - Sections Objects: \(sectionInfo.numberOfObjects)") 
    return sectionInfo.numberOfObjects 
} 

는 참고 용으로 만 사용된다 일부 인쇄 문이 있습니다 여기에

은 일부 코드입니다! 때문에 섹션/numberOfRows이 잘못 사실 것 같아요 -

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let myCell = myTable.dequeueReusableCell(withIdentifier: "myCell")! as UITableViewCell 
    let accountBalanceChanges = self.fetchedResultsController.object(at: indexPath) 
    print("AccountBalanceChanges from cell....") 
    print(accountBalanceChanges) 

    let details = accountBalanceChanges.accountchanges! as NSSet 
    print("Print out the details:") 
    print(details) 
    let detailSet = details.allObjects 
    let detailSetItem = detailSet.count // Just for information! 


    let myPrint = detailSet[indexPath.row] as! AccountChanges 
    let myVal = myPrint.category 

    myCell.textLabel?.text = myVal 

    return myCell 
} 

그래서, 나는 데이터 만 항상 하나 개의 항목 만이 아닌 전체 집합을 얻을 수 있어요. 여기

내 NSFRC

입니다
var fetchedResultsController: NSFetchedResultsController<Accounts> { 
    if _fetchedResultsController != nil { 
     return _fetchedResultsController! 
    } 
    let fetchRequest: NSFetchRequest<Accounts> = Accounts.fetchRequest() 
    // Set the batch size to a suitable number. 
    fetchRequest.fetchBatchSize = 20 
    // Edit the sort key as appropriate. 

    let sortDescriptor = NSSortDescriptor(key: "aName", ascending: false) 
    fetchRequest.sortDescriptors = [sortDescriptor] 


    let predicate = NSPredicate(format: "(ANY accountchanges.accounts = %@)", newAccount!) 
    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.coreDataStack.context, sectionNameKeyPath: nil, cacheName: nil) 
    aFetchedResultsController.delegate = self 
    _fetchedResultsController = aFetchedResultsController 

    do { 
     try _fetchedResultsController!.performFetch() 
    } catch { 
     // Replace this implementation with code to handle the error appropriately. 
     // fatalError() 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. 
     let nserror = error as NSError 
     fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 
    } 

    return _fetchedResultsController! 
} 

나는 그것이 SortDescriptor 또는 술어 가정입니다 - 아니면 둘 다?

어떤 도움이나 지시 사항이든 잘 부탁드립니다. 나는 여러 가지 접근법을 이미 시도했지만 아무도 정확한 결과를 제공하지 못했습니다.

let predicate = NSPredicate(format: "accounts.aId = %@", ACCOUNTID) 

또는

let predicate = NSPredicate(format: "accounts = %@", account.objectID) 

I :

+0

대부분 실패 메시지는 "keypath for (category, title ...)는 존재하지 않습니다"위의 코드에서 술어는 작동하는 것처럼 보이지만 SortDescriptor가 나에게 가장 많은 문제를주고 있습니다 ... 내 추측 –

답변

1

나는 특정 아이디의 계정에 대한 모든 변경 사항을 가져, 다음과 같은 선언문을 사용하는 FRC를 사용하여 의미 반대 할 것 Accounts 엔티티의 이름을 Account로 변경하고 1 대 1 관계이므로 관계에 대해 동일하게 변경합니다. 모든 계정이있는 테이블보기가 있다고 가정하고 계정을 클릭하면 변경 사항이 다시 제공됩니다. "일대 다 키를 여기에 허용되지 않습니다"또는 : -

var fetchedResultsController: NSFetchedResultsController<AccountChanges> { 
    if _fetchedResultsController != nil { 
     return _fetchedResultsController! 
    } 
    let fetchRequest: NSFetchRequest<AccountChanges> = AccountChanges.fetchRequest() 
    // Set the batch size to a suitable number. 
    fetchRequest.fetchBatchSize = 20 
    // Edit the sort key as appropriate. 

    let sortDescriptor = NSSortDescriptor(key: "aName", ascending: false) 
    fetchRequest.sortDescriptors = [sortDescriptor] 


    let predicate = NSPredicate(format: "accounts.aId = %@", ACCOUNTID) 
    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.coreDataStack.context, sectionNameKeyPath: nil, cacheName: nil) 
    aFetchedResultsController.delegate = self 
    _fetchedResultsController = aFetchedResultsController 

    do { 
     try _fetchedResultsController!.performFetch() 
    } catch { 
     // Replace this implementation with code to handle the error appropriately. 
     // fatalError() 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. 
     let nserror = error as NSError 
     fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 
    } 

    return _fetchedResultsController! 
} 

건배

한 가지 더, 나는 SortDescriptor와 술어/또는 NSFRC의 sectionNameKeyPath에서 서로 다른 경로를 많이 시도
+0

그뿐만 아니라 그다지 나는 생각하지 않았습니다. 모든 계정 변경에 accountID를 전달할 수 있으므로 관계가 필요하지 않습니다. 그러나 우리는 관계를 사용할 수있는 옵션을 갖고 있기 때문에 그렇게하고 싶었습니다. 어쨌든, 다른 해결책이 없다면 그렇게 할 수도 있고 리팩터링 할 수도 있습니다. 나는 이미 내 데이터베이스에 고유 한 ID를 가지고 있으며 그렇게 할 수는 있지만 관계와 NSFRC와 함께 작동시키는 것이 좋을 것입니다. 제안 해 주셔서 감사합니다 ... –

+0

제 제안은 여전히 ​​NSFRC와 관계가 있습니다. 당신은 단지 다른 방향으로 가져와야합니다.계정을 가져 와서 변경 사항에 액세스하려고 시도하지 말고, 술어에 전달한 ID를 가진 계정의 모든 변경 사항을 가져 오십시오. – ubiAle

+0

그게 효과가 작동하지 않습니다, 그것은 계정에 계정 변경에서 일대일입니다 - 하나의 계정 변경이 하나의 계정에만 영향을 미칠 수 있으므로 변경할 수 없습니다. 계정에서 계정 변경에 이르기까지 일대 다 방식 만 있습니다. 그리고 위에서 쓴 것처럼 술어는 문제가 아니며 NSFRC의 SortDescriptor 또는 keyPath입니다. 올바른 계정을 얻었지만 섹션 수/numberOfRows에서 변경 횟수가 잘못되었습니다 .... 그리고 전에 말했듯이 NSFRC 없이는 모든 작업을 수행 할 수 있습니다. 따라서 문제는 가져 오기 자체가 아니라 NSFRC로 가져 오는 것입니다. 당신이 NSSet 형식을 다시 얻었 기 때문에. –

관련 문제