处理长时间运行的任务和Parse.com API

时间:2015-01-28 01:09:18

标签: ios swift parse-platform

我是iOS的新手,所以我不知道如何处理我的问题。

我担心的问题是当我的应用中的用户说他们想要删除他们的帐户时,我会删除Parse中后端的所有数据。我必须通过几个表来删除用户数据,并根据可能需要一些时间的数据量。在此期间,用户可以随时将应用程序置于后台,因为他们不想等待或等等。

这是我做的一个例子

var commentKeys:Comment.CommentKeys = Comment.CommentKeys()
        var qComment = Comment.query()
        qComment.whereKey("id", equalTo: account.getId())
        qComment.findObjectsInBackgroundWithBlock {(results: [AnyObject]!, error:NSError!) -> Void in
            if(error == nil){
                if((results as NSArray).count > 0){
                    for item in (results as NSArray){
                        (item as Comment).deleteInBackgroundWithBlock(nil)
                    }
                }
            }
        }

这只是一张桌子,我需要清除6个。

如果这些都不清楚,可能导致数据孤立。我怎样才能防止这种情况,即使应用程序已经背景化,有没有办法让执行完成?

Implementing long running tasks in background IOS但是使用NSOperationQueue并且我不是

1 个答案:

答案 0 :(得分:2)

这里有两件事。

1。)查看解析云代码。您将能够创建deleteThisUser(objectId)函数,该函数将允许您删除服务器上的用户和所有相关数据。

2。)您可能希望更有效地组织表格或更有效地查询表格。使用Parse SDK,您可以在同一请求中查询多个对象并销毁()多个对象。

您可以将相关对象存储为对象中的指针。 例如,您可以将与注释相关的帖子存储为注释对象中的指针。如果帖子是由用户编写的,也可以删除。这是一个不好的例子,但这应该显示概念,以便您可以将其应用到其他地方。

NSMutableArray *objectsToBeDeleted = [NSMutableArray array];

PFQuery *query = [PFQuery queryWithClassName:@"Comment"];
 // Retrieve the most recent ones
 [query orderByDescending:@"createdAt"];
 [query whereKey:@"id", equalTo: account.getId()];

 // Include the post data with each comment
 [query includeKey:@"post"];

 [query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
    for (PFObject *comment in comments) {

        // add comment to be deleted
        [objectsToBeDeleted addObject:comment];

        //get the post from the pointer contained in the comment object
        PFObject *post = comment[@"post"];

        //Check to see if the post was written by the current user
        if(post[@"authorId"] ==  account.getId()){
             //add the post to be deleted also
             [objectsToBeDelted addObject:post];
        }

    }
    // Accepts an NSArray containing one or more PFObject instances
    [PFObject deleteAllInBackground:objectsToBeDeleted];
}];

我想deleteAllInBackground:方法将接受NSMutableArray,但如果没有,你可以使用这段代码:

NSArray *arrayToDelete = [objectsToBeDeleted copy];
// Accepts an NSArray containing one or more PFObject instances
[PFObject deleteAllInBackground:arrayToDelete];
祝你好运

相关问题