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

Handle long running tasks and Parse.com API

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

我担心的问题是,当我的应用程序中的用户说他们想要删除他们的帐户时,我从 Parse 中保存的后端删除了所有这些数据。我必须经历几个 tables 才能删除用户数据,这取决于数据量,这可能需要一些时间。在此期间,用户可以随时将应用程序置于后台,因为他们不想等待或其他原因。

这是我所做的一个例子

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)
                    }
                }
            }
        }

这只是一个 table,还有 6 个我必须清除。

如果这些都没有清除,可能会导致数据孤立。我怎样才能防止这种情况发生,有没有办法让执行完成,即使应用程序已在后台运行?

Implementing long running tasks in background IOS 但那是在使用 NSOperationQueue 而我不是

这里有两件事。

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

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

您可以将相关对象存储为对象内的指针。 例如,您可以将与评论相关的 post 存储为评论对象中的指针。如果post是用户写的那也可以删除。这是一个不好的例子,但这应该展示了这个概念,所以你可以在其他地方应用它。

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];

祝你好运