正在主线程上执行长时间运行的Parse操作?

时间:2014-12-30 19:26:50

标签: ios parse-platform

正在主线程上执行长时间运行的Parse操作正在从下面发生在这部分代码上,我似乎无法弄清楚如何消除错误。关于如何摆脱警告的任何帮助。

PFFile *image = (PFFile *)[wallObject objectForKey:KEY_IMAGE];
UIImageView *userImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]];
userImage.frame = CGRectMake(0, 65, wallImageView.frame.size.width, 225);
[wallImageView addSubview:userImage];

这是下面的代码

-(void)loadWallViews
{
    //Clean the scroll view
    for (id viewToRemove in [self.wallScroll subviews]){

        if ([viewToRemove isMemberOfClass:[UIView class]])
            [viewToRemove removeFromSuperview];
    }

    //For every wall element, put a view in the scroll
    int originY = 10;

    for (PFObject *wallObject in self.imageFilesArray){

        //Build the view with the image and the comments
        UIView *wallImageView = [[UIView alloc] initWithFrame:CGRectMake(10, originY, self.view.frame.size.width - 20 , 330)]; //self.view.frame.size.height
        //[[UIView appearance] setBackgroundColor:[UIColor redColor]]; //added for problem solve

        //Add the image
        PFFile *image = (PFFile *)[wallObject objectForKey:KEY_IMAGE];

        UIImageView *userImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]];
        userImage.frame = CGRectMake(0, 65, wallImageView.frame.size.width, 225);
        [wallImageView addSubview:userImage];

        UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, wallImageView.frame.size.width,55)];
        infoLabel.text = [wallObject objectForKey:@"newsTitle"];
        infoLabel.font = [UIFont fontWithName:KEY_FONT size:20];
        infoLabel.textColor = [UIColor darkGrayColor];
        infoLabel.backgroundColor = [UIColor clearColor];
        infoLabel.numberOfLines = 0;
        [wallImageView addSubview:infoLabel];

        //Add the info label (User and creation date)
         NSDate *creationDate = wallObject.createdAt;
         NSDateFormatter *df = [[NSDateFormatter alloc] init];
         [df setDateFormat:KEY_DATEFORMAT];

        //Add the comment
        UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, wallImageView.frame.size.width, 12)];
        //commentLabel.text = [NSString stringWithFormat:@"Uploaded by: %@, %@", [wallObject objectForKey:@"NewsDetail"], [df stringFromDate:creationDate]];
        commentLabel.text = [NSString stringWithFormat:@": %@, %@", [wallObject objectForKey:@"newsDetail"], [df stringFromDate:creationDate]];
        commentLabel.font = [UIFont fontWithName:KEY_FONT size:10];
        commentLabel.textColor = [UIColor lightGrayColor];
        commentLabel.backgroundColor = [UIColor clearColor];
        [wallImageView addSubview:commentLabel];

        UILabel *readLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 71 , 50, wallImageView.frame.size.width, 12)];
        readLabel.text = @"Read more";
        readLabel.font = [UIFont fontWithName:KEY_FONT size:10];
        readLabel.textColor = [UIColor blueColor];
        readLabel.backgroundColor = [UIColor clearColor];
        [wallImageView addSubview:readLabel];

        UIButton *faceBtn = [[UIButton alloc] initWithFrame:CGRectMake(2,310, 20, 20)];
        [faceBtn setImage:[UIImage imageNamed:@"Facebook.png"] forState:UIControlStateNormal];
        [faceBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [wallImageView addSubview:faceBtn];

        UIButton *twitBtn = [[UIButton alloc] initWithFrame:CGRectMake(32,310, 20, 20)];
        [twitBtn setImage:[UIImage imageNamed:@"Twitter.png"] forState:UIControlStateNormal];
        [twitBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [wallImageView addSubview:twitBtn];

        UIButton *tumblrBtn = [[UIButton alloc] initWithFrame:CGRectMake(62,310, 20, 20)];
        [tumblrBtn setImage:[UIImage imageNamed:@"Tumblr.png"] forState:UIControlStateNormal];
        [tumblrBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [wallImageView addSubview:tumblrBtn];

        UIButton *yourBtn = [[UIButton alloc] initWithFrame:CGRectMake(92,310, 20, 20)];
        [yourBtn setImage:[UIImage imageNamed:@"Flickr.png"] forState:UIControlStateNormal];
        [yourBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [wallImageView addSubview:yourBtn];
     //   wallImageView = UIEdgeInsetsMake(0.0f, myCell.frame.size.width, 0.0f, 400.0f);
        UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 343, self.view.frame.size.width, .8)];
        separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
        [wallImageView addSubview:separatorLineView];

        [self.wallScroll addSubview:wallImageView];

        originY = originY + wallImageView.frame.size.width + 1;

    }

    //Set the bounds of the scroll
    self.wallScroll.contentSize = CGSizeMake(self.wallScroll.frame.size.width, originY);

    //Remove the activity indicator
    [self.activityIndicator stopAnimating];
    [self.activityIndicator removeFromSuperview];
}

1 个答案:

答案 0 :(得分:0)

您应首先检查数据是否可用,如果可用,请使用它,如果没有,请检索它。你仍然会收到一个警告,这是一个很长的操作(如果你的PFFile中有大量的数据,可能就是这样),但它是安全的。如果您只是通过网络检索数据块,那么您会没事的。

// PFFile *imageFile
UIImage *image;
if ([imageFile isDataAvailable]) {
    image = [UIImage imageWithData:[imageFile getData]];
} else {
    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (error) {
            // error
        } else {
            if (data != nil) {
                image = [UIImage imageWithData:data];
            }
        }
    }];
}

想要做的是阻止检索PFFile的数据。如果数据在PFFile中可用,则可以安全地调用getData而不使用块。