NSManagedObject在cellForItemAt indexPath中变为nil

时间:2017-05-01 12:07:14

标签: ios swift core-data

我正在使用带有水平滚动的collectionView.At viewdidload我正在获取coredata对象。

var listOfArticles  = [Article]()

  override func viewDidLoad() {
        super.viewDidLoad()
     listOfArticles =   CoreDataManager().fetchArticleWithCategory(category: "Entertainment", limit: 10)!
}

当我以前在viewdidload中打印listOfArticles时。

for newData in listOfArticles {
            print(newData.article_id)
        }
  

输出: -

1599845
1599844
1599841
1599838
1599836
1599827
1599823
1599820
1599813
1599811
1599806

在indexPath.item == 0我能够获取Article objet但是indexPath.item == 1 Article objet变为null。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell :NewsCollectionViewCell  = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! NewsCollectionViewCell
        var articles = listOfArticles[indexPath.item]
        cell.currentArticleToShow = articles
        if let newVar = articles.articleToSectionRel?.allObjects {
            cell.currentSections = newVar as! [Section]
        }
    }

当我尝试在indexPath.item == 0

打印 po文章
  

输出:

 <Article: 0x60800009f2c0> (entity: Article; id: 0xd000000000100024 <x-coredata://BA36E40C-6783-4CBF-8C2F-F596ABF1CD56/Article/p4> ; data: {
    articleToSectionRel = "<relationship fault: 0x600000426a00 'articleToSectionRel'>";
    "article_id" = 1599845;
    "book_mark" = 0;
    category = Entertainment;
    "image_url" = "http://images.newindianexpress.com/uploads/user/imagelibrary/2017/4/20/original/KatyPerry-AP1.png";
    "modiffied_date" = "2017-05-01 16:52:00";
    "sub_category" = English;
    summary = "http://images.newindianexpress.com/uploads/user/imagelibrary/2017/4/20/original/KatyPerry-AP1.png";
    title = "Katy Perry courts controversy\U00a0comparing her black hair to Barack Obama";
    topStorie = 0;
})

当我尝试在indexPath.item == 1

打印 po文章
  

输出:

<Article: 0x608000280370> (entity: <null>; id: 0xd000000000180024 <x-coredata://BA36E40C-6783-4CBF-8C2F-F596ABF1CD56/Article/p6> ; data: <fault>)

当我在listOfArticles中使用cellForItemAt时。我在indexPath.item == 1获得了文章对象。

  listOfArticles =   CoreDataManager().fetchArticleWithCategory(category: "Entertainment", limit: 10)!

为什么Article对象在对象上变为null indexPath.item == 1当我将数据提取到viewdidload中时?提前致谢。

0 个答案:

没有答案