0

the for loop looks like this , which i have written in view did load, so it takes more time to load this page.

for (int i=3; i<[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]count]; i++)
{


    if (i%3==0)
    {
        x=0;
        y++;
    }

    view=[[UIView alloc]initWithFrame:CGRectMake((x*250)+5, (y*404)+6, 244, 400)];
    [view setBackgroundColor:[UIColor whiteColor]];
    view.layer.borderColor=[[UIColor whiteColor]CGColor];
    view.layer.borderWidth=1.0;
    view.layer.cornerRadius = 5;
    view.layer.masksToBounds = YES;
    [scroller addSubview:view];

titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 230, 20)];
    [titlelabel setText:[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"text"]];
    [titlelabel setNumberOfLines:0];
    titlelabel.font=[UIFont boldSystemFontOfSize:15.0f];




    [titlelabel setBackgroundColor:[UIColor clearColor]];
    [titlelabel sizeToFit];
    [view addSubview:titlelabel];

    datelabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 62, 190, 20)];
    [datelabel setText:[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"text"]];
    [datelabel setNumberOfLines:0];
    datelabel.font=[UIFont fontWithName:@"arial" size:12.0f];
    [datelabel setBackgroundColor:[UIColor clearColor]];
    [datelabel sizeToFit];
    [view addSubview:datelabel];

    NSData *data=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"image"]objectForKey:@"text"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]];
    NSLog(@"data= %@",data);
    if (data==NULL ||[[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"image"]objectForKey:@"text"] isEqualToString:@""])
    {
        textview=[[UITextView alloc]initWithFrame:CGRectMake(2,80, 238, 386)];
        [textview setText:[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"text"]];
        [textview setFont:[UIFont fontWithName:@"ArialMT" size:14]];
        [textview setDelegate:self];
        [view addSubview:textview];
    }
    else
    {
        imageview=[[UIImageView alloc]initWithFrame:CGRectMake(7, 80, 230, 150)];
        [imageview setImage:[UIImage imageWithData:data]];
        [view addSubview:imageview];

        textview=[[UITextView alloc]initWithFrame:CGRectMake(5, 240, 238, 200)];
        [textview setText:[[[[[[dataDict objectForKey:@"rss"]objectForKey:@"channel"]objectForKey:@"item"]objectAtIndex:i]objectForKey:@"title"]objectForKey:@"text"]];
        [textview setFont:[UIFont fontWithName:@"ArialMT" size:14]];
        [textview setDelegate:self];
        [view addSubview:textview];
    }

}

here wat make me problem is the image which gets from server every time so this gets slower, pls suggest how to make it do as lazy loading ....

thanks in advance

KSR
  • 99
  • 1
  • 12
  • How many items would normally be in rssItems, roughly? – James Aug 27 '13 at 09:33
  • http://stackoverflow.com/a/11728134/1059705 and this http://jeffreysambells.com/2013/03/01/asynchronous-operations-in-ios-with-grand-central-dispatch – Bala Aug 27 '13 at 09:34
  • @James its dynamic , it can be sumtime 10 sumtime 50 also ... – KSR Aug 27 '13 at 09:39
  • @Bala async method will do it in background i know but i add my view to the scroller in for loop only, how it will wrk 1 by 1 wen its done ? – KSR Aug 27 '13 at 09:41
  • I'm surprised it's so slow with that number of items. Have you thought about using a UICollectionView instead? You'd be able to create a similar grid layout and wouldn't have to worry about loading the data yourself -- it's very similar to using a UITableView. – James Aug 27 '13 at 09:59
  • @james actually my layout design willl be looking like 1st row should hav 2 cells in it of different size , from 2nd row 3 cells of same size is it possible in uicollection view? – KSR Aug 27 '13 at 10:02
  • Yes, you can have different sizes for different cells. There's a good tutorial here: http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12 – James Aug 27 '13 at 10:04
  • but uicollectionview will only work ios>6 rite? – KSR Aug 27 '13 at 10:09
  • @KSR Given that as of June 2013, over 93% of users had adopted iOS 6, do you really need to support prior releases? http://www.imore.com/ios-6-used-93-ios-customers – bbum Aug 27 '13 at 16:00
  • possible duplicate of [Asynchronus way of image in for loop](http://stackoverflow.com/questions/18510909/asynchronus-way-of-image-in-for-loop) – Divya Bhaloidiya Aug 29 '13 at 12:45

1 Answers1

0

UICollectionView should be a better choice to display that kind of data in my opinion, but it's not compatible with ios5.

If you need an ios 5 compatibility, check out that answer: https://stackoverflow.com/a/16039194/2707614

Finally, to respond to your question, you can try to use Grand Central Dispatch as explained here

It should look like that (I don't have a computer to test):

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

 dispatch_async(queue, ^{
for (int i=3; i<[[[self rssParser]rssItems]count]; i++)
    // for (int i=3; i<[titlearray count]; i++)
{
    if (i%3==0)
    {
        x=0;
        y++;
    }

    view=[[UIView alloc]initWithFrame:CGRectMake((x*250)+10, (y*306)+105, 244, 300)];
    [view setBackgroundColor:[UIColor whiteColor]];
    view.layer.borderColor=[[UIColor whiteColor]CGColor];
    view.layer.borderWidth=1.0;
    view.layer.cornerRadius = 5;
    view.layer.masksToBounds = YES;



    titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 230, 20)];
    [titlelabel setText:[[[[self rssParser]rssItems]objectAtIndex:i-1]title]];
    [titlelabel setNumberOfLines:0];
    titlelabel.font=[UIFont boldSystemFontOfSize:14.0f];

    [titlelabel setBackgroundColor:[UIColor clearColor]];
    [titlelabel sizeToFit];


    datelabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 62, 190, 20)];
    [datelabel setText:[[[[self rssParser]rssItems]objectAtIndex:i-1]pubDate]];
    [datelabel setNumberOfLines:0];
    datelabel.font=[UIFont fontWithName:@"arial" size:10.0f];
    [datelabel setBackgroundColor:[UIColor clearColor]];
    [datelabel sizeToFit];

    x++;

    dispatch_sync(dispatch_get_main_queue(), ^{
        [scroller addSubview:view];
        [view addSubview:titlelabel];
        [view addSubview:datelabel];
    });

}
});
Community
  • 1
  • 1
Alban
  • 1,624
  • 11
  • 21
  • You should not be mucking about with UI objects from background queues. That *generally* includes creating them (unless that has changed in a recent release). As well, note that doing this assumes that `rssParser`, etc, are all thread safe... – bbum Aug 27 '13 at 16:01
  • I used to create ui objects in background queue and call the "display related" methods on main thread with no problem, but i'm interested knowing why it's bad to only create objects. – Alban Aug 27 '13 at 18:17
  • From the documentation: ***Note:** For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.* I.e. it only works by coincidence. – bbum Aug 27 '13 at 20:09
  • Thank you for your answer, it is part of the documentation I have read. But for me, it does not mean you can not create objects in a background thread ... I think it is not a coincidence and that it works because the drawRect method is not called until the view is not added. I think I'll create a question on SO for that, because i'm not sure. Thank you, this will be my first! – Alban Aug 27 '13 at 21:44
  • 1
    It doesn't matter that it works for you; that it works is merely coincidental. That it is documented explicitly as to be avoided means that it may not work after any given software update or on a particular hardware configuration (or system configuration -- localization, for example, can have significant impact on the changes to the behavior of UITextField). – bbum Aug 27 '13 at 22:26
  • thanks bbum for your response. As I said, I've created a question here: http://stackoverflow.com/questions/18476540/does-the-creation-of-an-uikit-object-in-background-using-gcd-is-a-bad-practice – Alban Aug 27 '13 at 22:32