Cocoa Touch - display activity indicator when loading UITabBar View

I have a UITabBar application with two views that download large amounts of data from the Internet in their viewWillAppear methods. I want to show a progress bar or an activity indicator while this data is being extracted to make sure that the user knows that the application is not frozen.

I know this was asked before. I just need to clarify what seems like a good solution.

I applied the code in the example. The question of the original author of the question later solved his problem by putting the data search in another "thread". I understand the concept of threads, but I don’t know how this would affect it.

In the course of research, I found that I need to transfer all my heavy data search to the background thread, since all user interface updates occur in the main thread.

If it were so kind to provide an example for me, I would be very grateful. If necessary, I can provide part of my existing code.

+3
source share
1 answer

If you use NSURLConnection, it starts in another thread automatically.

in your view of DidLoad:

NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];

. -connection Esc, , . :

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    // this is called when there is a response
        // if you're collecting data init your NSMutableData here
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // each time the connection downloads a 
        // packet of data it gets send here
        // so you can do [myData appendData:data];
} 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        // the connection has finished so you can 
        // do what you want with the data here
}

, . NSURLConnection , . , , .:)

+3

Source: https://habr.com/ru/post/1781406/


All Articles