0

지난 몇 일 동안 컬렉션보기가있는보기에서 'newCollection'단추를 눌러 UICollectionView에 새 데이터 이미지를 채우려고 시도했습니다. 내 콘솔에서 이미지 데이터가 다운로드되고있는 것을 볼 수 있습니다. CV 셀이 뷰를 벗어나면 다시 돌아와서 볼 수 있습니다. 콜렉션 뷰 셀이 실시간으로 업데이트되지 않는 이유는 무엇입니까? 여기UICollectionView NSFetchedResultsController가 처리 한 셀이 이미지를 다시 채우지 않습니다.

내가 함께 일하고 있어요 무엇 :

https://github.com/tdangles81/Virtual-Tourist/tree/detail-view

@IBAction func newCollectionBtn(sender: AnyObject) { 
    getNewCollection() 
} 

func getNewCollection(){ 
    if let newFetch = self.fetchedResultsController.fetchedObjects{ 
     for object in newFetch{ 
      let newImg = object as! ImgModel 
      self.sharedContext.deleteObject(newImg) 
     } 
    } 
    FlickrRequestClient.sharedInstance().getNewGeoImgs(selectedPin) 
} 

var sharedContext: NSManagedObjectContext { 
    return CoreDataStack.sharedInstance().managedObjectContext 
} 

func saveContext(){ 
    return CoreDataStack.sharedInstance().saveContext() 
} 

lazy var fetchedResultsController: NSFetchedResultsController = { 

    let fetchRequest = NSFetchRequest(entityName: "Image") 
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)] 
    fetchRequest.predicate = NSPredicate(format: "pin == %@", self.selectedPin) 

    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, 
                   managedObjectContext: self.sharedContext, 
                   sectionNameKeyPath: nil, cacheName: nil) 
    return fetchedResultsController 
}() 

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin") as? MKPinAnnotationView 
    if pinView == nil{ 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin") 
    }else{ 
     pinView?.annotation = annotation 
    } 
    return pinView 
    } 


func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return fetchedResultsController.sections?.count ?? 0 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    let sectionInfo = self.fetchedResultsController.sections![section] 
    print(sectionInfo.numberOfObjects) 
    return sectionInfo.numberOfObjects 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("DetailCell", forIndexPath: indexPath) as! ImageCollectionCell 
    let image = fetchedResultsController.objectAtIndexPath(indexPath) as! ImgModel 

    configureUI(cell, image: image, atIndexPath: indexPath) 
    return cell 
} 

func configureUI(cell: ImageCollectionCell, image: ImgModel, atIndexPath indexPath: NSIndexPath) { 

    if image.image != nil{ 
     image.loadUpdateHandler = nil 
     cell.flickrImageView.image = image.image! 
     print("Image.image \(image.image!)") 
     self.saveContext() 
    }else{ 
     image.loadUpdateHandler = {[unowned self]() -> Void in 
     dispatch_async(dispatch_get_main_queue(), { 
     self.collectionView.reloadData() 
      }) 
     } 
     cell.flickrImageView.image = image.image 
    } 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){ 
    if collectionView.cellForItemAtIndexPath(indexPath) != nil { 
     let image = fetchedResultsController.objectAtIndexPath(indexPath) as! ImgModel 
     print(image) 
    } 
} 

func controllerWillChangeContent(controller: NSFetchedResultsController) { 

    insertedIndex = [NSIndexPath]() 
    updatedIndex = [NSIndexPath]() 
    deletedIndex = [NSIndexPath]() 
} 

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { 
    switch type { 
    case .Insert: 
     insertedIndex.append(newIndexPath!) 
    case .Update: 
     updatedIndex.append(indexPath!) 
    case .Move: 
     print("Surprise!") 
    case .Delete: 
     deletedIndex.append(indexPath!) 
    } 
} 

func controllerDidChangeContent(controller: NSFetchedResultsController) { 
    collectionView.performBatchUpdates({() -> Void in 
     for indexPath in self.insertedIndex { 
      self.collectionView.insertItemsAtIndexPaths([indexPath]) 
     } 
     for indexPath in self.updatedIndex { 
      self.collectionView.reloadItemsAtIndexPaths([indexPath]) 
     } 
     for indexPath in self.deletedIndex { 
      self.collectionView.deleteItemsAtIndexPaths([indexPath]) 
     } 
     },completion: nil) 

} 

func addSpinner(cellView: UICollectionViewCell, activityBool: Bool){ 

    let activitySpinner =  UIActivityIndicatorView.init(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge) 
    activitySpinner.center = cellView.center 
    activitySpinner.color = UIColor.whiteColor() 
    activitySpinner.startAnimating() 

    if activityBool == true{ 
     activitySpinner.startAnimating() 
     cellView.addSubview(activitySpinner) 
    }else if activityBool == false{ 
     activitySpinner.stopAnimating() 
     cellView.willRemoveSubview(activitySpinner) 
    } 
} 

func removeSpinner(){ 

} 

} 
+0

저해상도 이미지 사용 시도 – JAck

+0

이 트릭을 수행하지 못했습니다. 나는 그것이 내 UI를 메인 스레드에서 업데이트하는 것과 관련이 있다고 생각한다. 여기에서 볼 마음? https://github.com/tdangles81/Virtual-Tourist/tree/detail-view –

답변

1

내가 NSFetchedResultsController 행동에 문제를 경험했다. NSPredicate가 가져 오기 요청에서 설정되면 대리자 메서드는 호출되지 않습니다.

이 문제가 있는지 확인하기 위해 조건자를 제거하십시오.

Xcode의 버그인지, 오류와 관련된 일부 실수인지 또는 NSFetchedResultsController 클래스에서 새 쿼리 토큰 시스템이 사용되고 있는지 확인하려고합니다.

+0

문제가 해결되었지만 결국 fetchedResultsController 대리자를 self로 설정하지 않았다는 것을 알았습니다. –