异步获取Swift Xcode [英] Asynchronous Fetching Swift Xcode

查看:283
本文介绍了异步获取Swift Xcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用集合视图来显示存储在CoreData中的图像,只有几个(大约15),但当我向下滚动它落后,它接缝是因为我不是抓取我的数据不同寻常,是可能的只有15 UIImages ?

I use a collection view to display images stored in CoreData, only a few (around 15) but when I scroll down it lags, It seams to be because I am not fetching my data asyncrhronously, is it possible with only 15 UIImages?

所以这里是我的问题我找不到一个下降教程在异步提取swift任何地方,3天我在寻找。对不起,如果我没有搜索。

So here is my problem I cannot find a descent tutorial on asynchronous fetching in swift anywhere ,3 days I am looking. Sorry if I didn't search well.

这是从一些教程

    let entity = NSEntityDescription.entityForName("Dreams", inManagedObjectContext: contxt)

    let fetchRequest = NSFetchRequest()
    fetchRequest.entity = entity

    let asyncFetch = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) {
        (result:NSAsynchronousFetchResult!) -> Void in

        if result.finalResult!.count > 0 {
            self.dreams = result.finalResult! as [Dream]
        }
        NSLog("Done.")
    }


$ b b

Dreams是一个类型为[Dream]的数组,但是当我显示集合视图没有出现
这是我以前做的

Dreams is an array of type [Dream] but when I display the collection view nothing appears This is how I did it previously

    let fetchRequest = NSFetchRequest(entityName: "Dreams")

    if let fetchResults = contxt.executeFetchRequest(fetchRequest, error: nil) as? [Dream] {
        dreams = fetchResults
    }

非常感谢您的帮助

推荐答案

在这种情况下没有时间提供特定的代码。但我确实想提出一个基本的策略。在主线程上获取极不可能是你的问题,但是一次抓取一切肯定是可能的,特别是对于大型二进制数据。如果:

Unfortunately, I don't have time to offer specific code in this instance. I did want to present a basic strategy, however. Fetching on the main thread is exceedingly unlikely to be your problem here, but fetching everything at once definitely could be, particularly with large binary data involved. This should be sped up significantly if:


  1. 您开始使用 NSFetchedResultsController 良好的批量大小,而不是原始获取请求。 NSFetchedResultsController对集合视图的更新有一点小小的方式超出了这个问题的具体范围,但是一旦设置正确,它们工作得很好。

  2. 您将图像存储为文件,而不是存储在Core Data本身中,而是存储文件的路径。然后,每当您在对象中出现错误时,图像数据将无法加载到内存中,您可以使用该文件网址在屏幕上轻松地加载和显示图像。

  1. You start using an NSFetchedResultsController with a good batch size rather than a raw fetch request. NSFetchedResultsController is a little finicky about updates with collection views, in ways which are beyond the specific scope of this question, but they work very well once set up correctly.
  2. You store the images as files instead of in Core Data itself, and store the path to the file instead. Then that image data won't get loaded into memory every time you fault in the object, and you can simply load and display the images as the cells come on screen using that file URL.

坚持在Core Data中存储二进制数据的人通常会发现,将数据作为唯一的属性存储在一个单独的Image实体中,并与您显示的实体有关系。即使当为集合视图提取的对象在内存中时,图像对象(连同其二进制图像数据)也可以保持故障,从而不在内存中。然而,我不喜欢将它们存储在Core Data中,而只是处理产生的原子性问题以及Core Data指向的文件被删除的可能性。

People who insist on storing binary data in Core Data usually find that storing the data as the only attribute in a separate "Image" entity with a relationship to the entity you are displaying helps. The image object (along with its binary image data) can then be kept a fault, and thus not in memory, even when the object you fetch for the collection view is in memory. I, however, prefer not to store them in Core Data, and simply handle the resulting atomicity gotchas and the possibility the file Core Data points to got deleted somehow.

这篇关于异步获取Swift Xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆