1

I am working on a shopping app where I use web services to fetch data i.e. product list etc. But it takes too much time to load and thus makes my app really slow.

Is there any solution to this problem?

below is the code I've tried to get Product List.

NSURL * Url=[NSURL URLWithString:@"URL/api/product"]; 
NSData * Data=[NSData dataWithContentsOfURL:Url];
NSString *str=[[NSString alloc]initWithData:Data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[strinng dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil]; 

cannot put JSON response because it's too much.

Shikha Sharma
  • 451
  • 1
  • 5
  • 25

5 Answers5

4

There is no RIGHT or WRONG way way for this. But there are some points which we can consider for fast data loading. I can mention these points and there can be many other points as well:

  1. Use AFNetworking (for objective c) or Alamofire (for swift), for calling web services and getting response.
  2. Don't do too much things in viewDidLoad. Try to do as less as operations.
  3. You can call web service in viewDidLoad or in viewWillAppear(it will get called every time you visit screen)
  4. After getting response update UI on main thread. Refer this post.
  5. If you want to show images from URL's, load it asynchronously. You can use third party library like SDWebImage Or else use extensions provided in AFNetworking and Alamofire
  6. If you want to show list of products in list, use Load More functionality. for this purpose your API's should be smart enough which are implemented using pagination.
  7. Use UITableView or UICollectionView for showing reusable components.

Till now I can highlight these points. If I found anything else, I will update my answer.

Community
  • 1
  • 1
Pushkraj Lanjekar
  • 2,254
  • 1
  • 21
  • 34
1

It will be better if you show what you did for that, but according to your requirement i will suggest you,

  1. Use AFNetworking to communicate with server, it is little bit faster.

  2. if you send request to server in new ViewController, make sure you are going to send request in viewWillAppear.

  3. Try to avoid get too much data at the same time.

  4. Try to use Asynchronous request means that it will not wait. You use Asynchronous means that it will not wait. means the thread that initiated that operation will wait for the task to finish before continuing.

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
  • If you call it in viewWillAppear, it will get called every time you visit screen. If I am going to next screen and coming back then there is not need of calling API again. So I don't agree with #2 – Pushkraj Lanjekar Jan 04 '17 at 09:56
  • Then what will suggest you sir, i mean where to put that sending request, because she need to display that data in TableView..... @Pushkraj – Suraj Sukale Jan 04 '17 at 10:00
1

Just use NSURLSession. It is good API from Apple. Just remember to use dispatch grand central get mainQueue in the completion block to update any UI.

GeneCode
  • 7,545
  • 8
  • 50
  • 85
0

Start loading the data from API when the App Launches (AppDelegate)and store the response data in a model. This way the time lag would be reduced. Also you can try saving the response into CoreData and when the view is loaded you can refresh the data.

Hope this helps.

Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
  • I have different modules , like product list will display all the details of product and cart list where all the products are spontaneously added or deleted, so how can i add everything in app delegate ? :/ little confused , can you explain it a bit more :) – Shikha Sharma Jan 04 '17 at 09:27
  • Then you have to work with core data. I meant initialize the classes to get data from the server in the AppDelegate so the data fetching starts much before you open a VC. In your case coreData would be the way to go. – Md. Ibrahim Hassan Jan 04 '17 at 09:29
  • Okay, I really do not know much about core data, i'll google it :P but do you have any link which can make it easy for me to go through this whole process ? – Shikha Sharma Jan 04 '17 at 09:30
  • Saving them in CoreData and retrieving will be even slower – Matt Jan 04 '17 at 09:31
  • Save into CoreData Once so that you never get a blank Screen and then get data in the background and update. Gives better UX. @Matt – Md. Ibrahim Hassan Jan 04 '17 at 09:33
  • @Md.IbrahimHassan yes, it will look better. Since his API call takes lot of time there's a very good possibility of user navigating to details of a product, which might never exist currently and hence have to roll back and do all the reloading. – Matt Jan 04 '17 at 09:35
  • Using AFNetworking and loading data beforehand should solve the problem from our end then – Md. Ibrahim Hassan Jan 04 '17 at 09:58
0

Nothing can be done on your side to speed up the process when the data you receive is in abundant.

  1. You should split your APIs, probably, one API will give you the list of products.
  2. Make an API in way to send only request number of products and the number of products should be based on your phone screen resolution and number of products that can be shown there. For example, it could be 4 on iPhone 4 whereas 6 on iPhone 6. On scrolling call that API again to receive another 4 or 6 products.
  3. On clicking a particular product, use another API to get its contents.
Matt
  • 1,711
  • 18
  • 20
  • You are right but unfortunately i can't do all this :( all i can do is improve my code. – Shikha Sharma Jan 04 '17 at 09:29
  • You are not alone in this issue. Either request them to make changes or have to live with it :( – Matt Jan 04 '17 at 09:32
  • If you receive less data but takes huge time, then we can do something about it if you can provide the code. – Matt Jan 04 '17 at 09:33
  • NSURL * Url=[NSURL URLWithString:@"http://dealnxt.com/api/product"]; NSData * Data=[NSData dataWithContentsOfURL:Url]; NSString *str=[[NSString alloc]initWithData:Data encoding:NSUTF8StringEncoding]; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[strinng dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil]; this is the code i use to fetch product list and details – Shikha Sharma Jan 04 '17 at 09:42
  • there's too much data – Matt Jan 04 '17 at 09:52
  • yeah:( that's the thing – Shikha Sharma Jan 04 '17 at 10:41