开发者

Core Data: Executing fetch requests in loadView?

开发者 https://www.devze.com 2023-03-11 02:39 出处:网络
I have a UIViewController subclass which is having its view created programmatically within the loadView method.The primary purpose of the view is to display information that is fetched from a store v

I have a UIViewController subclass which is having its view created programmatically within the loadView method. The primary purpose of the view is to display information that is fetched from a store via core data. How the view is created will be different depending on how many entities are received from the fetch, so I need this information ahead of time. I believe all of the tutorials and sample code that I have seen so far shows fetch requests being executed in viewDidLoad (or later), so I just wanted to ask if there are an开发者_如何学Goy reasons to avoid doing this in loadView. Does it make any difference if I use fetches in loadView like so or in viewDidLoad? Thanks.

- (void)loadView {
    [super loadView];

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        ///
    }

    //...go on to load the view
}


If you are loading data once per setup-tear down cycle of the View Controller then putting it in loadView should be fine.

However if you have any buttons or UI inputs that may cause it to be reloaded, you may want to create your own reloadData method in your View Controller.

Take a look at some examples of UITableView with Core Data to get a feel for a common approach to loading core data objects in Cocoa. The UITableView uses the fly wheel pattern, meaning it reuses the table cell (row) objects and just inserts new data as it goes.

If you have lots of entities returned by CD, consider using a UITableView and you can style it up to look quite different from the norm if you need to.


The INIT sounds like it might be a good place to do this.


Performing fetch at viewDidLoad is fine. The most important thing is where the methods for customizing your view objects are called. Based on your explanation, data fetching should be done before, so just make sure your [super viewDidLoad] or other super class methods is not doing any view customizing before you actually fetch your data.

So, organizing your viewDidLoad like this will ensure you to have data fetched and prepared for view:

- (void)viewDidLoad {
    [super viewDidLoad] // 0: Make sure you are fully aware of what it's doing.

    // 1: Fetch your data

    // 2: Customize you view based on fetched data
}

I only use loadView, if I don't use nib file. Each method from viewController class has specific purpose, and for you to understand the architecture of your app, and eventually for others to understand it as well, respecting convention is very helpful. However, it's not enforced, so you can do whatever you would like to do.

If you really want to initialize your instance variables, separately from other view objects, I suggest you to use - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号