Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static func fetchCommentsAndTheirReplies(articleId: String, failure: ((NSError)->Void)?, success: (comments: [[String: AnyObject]], replies: [[[String: AnyObject]]], userIds: Set<String>)->Void) {
- var retComments = [[String: AnyObject]]()
- var retReplies = [[[String: AnyObject]]]()
- var retUserIds = Set<String>()
- let queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)
- Alamofire.request(.GET, API.baseUrl + API.article.listCreateComment, parameters: [API.article.articleId: articleId]).responseJSON {
- response in
- dispatch_async(queue) {
- guard let comments = response.result.value as? [[String: AnyObject]] else {
- failure?(Helper.error())
- return
- }
- print(comments)
- retComments = comments
- let group = dispatch_group_create()
- for (commentIndex, comment) in comments.enumerate() {
- guard let id = comment["_id"] as? String else {continue}
- let relevantUserIds = helperParseRelaventUserIdsFromEntity(comment)
- for userId in relevantUserIds {
- retUserIds.insert(userId)
- }
- retReplies.append([[String: AnyObject]]())
- dispatch_group_enter(group)
- Alamofire.request(.GET, API.baseUrl + API.article.listCreateReply, parameters: [API.article.commentId: id]).responseJSON {
- response in
- dispatch_async(queue) {
- if let replies = response.result.value as? [[String: AnyObject]] {
- for (_, reply) in replies.enumerate() {
- let relevantUserIds = helperParseRelaventUserIdsFromEntity(reply)
- for userId in relevantUserIds {
- retUserIds.insert(userId)
- }
- }
- retReplies[commentIndex] = replies
- }
- dispatch_group_leave(group)
- }
- }
- }
- dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
- success(comments: retComments, replies: retReplies, userIds: retUserIds)
- }
- }
- }
- public static void fetchCommentsAndTheirReplies(Context context, String articleId, final StringBuffer outErrorMessage, final Runnable failure, final ArrayList<JSONObject> outComments, final ArrayList<ArrayList<JSONObject>> outReplies, final HashSet<String> outUserIds, final Runnable success) {
- final RequestQueue queue = Volley.newRequestQueue(context);
- HashMap<String, String> commentParams = new HashMap<>();
- commentParams.put(API.article.articleId, articleId);
- JsonArrayRequest commentRequest = new JsonArrayRequest(Request.Method.GET, API.baseUrl + API.article.listCreateComment, new JSONObject(commentParams), new Response.Listener<JSONArray>() {
- @Override
- public void onResponse(JSONArray response) {
- try {
- for (int i = 0; i < response.length(); i++) {
- JSONObject comment = response.getJSONObject(i);
- outComments.add(comment);
- outUserIds.addAll(helperParseRelaventUserIdsFromEntity(comment));
- outReplies.add(new ArrayList<JSONObject>());
- //TODO: DISPATCH_GROUP?
- String id = comment.getString("_id");
- HashMap<String, String> replyParams = new HashMap<>();
- replyParams.put(API.article.commentId, id);
- final int finalI = i;
- JsonArrayRequest replyRequest = new JsonArrayRequest(Request.Method.GET, API.baseUrl + API.article.listCreateReply, new JSONObject(replyParams), new Response.Listener<JSONArray>() {
- @Override
- public void onResponse(JSONArray response) {
- try {
- for (int j = 0; j < response.length(); j++) {
- JSONObject reply = response.getJSONObject(j);
- outUserIds.addAll(helperParseRelaventUserIdsFromEntity(reply));
- outReplies.get(finalI).add(reply);
- }
- } catch (JSONException ex) {}
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {}
- });
- queue.add(replyRequest);
- }
- success.run();
- } catch (JSONException ex) {}
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- outErrorMessage.append(error.getMessage());
- failure.run();
- }
- });
- queue.add(commentRequest);
- }
- taskCount++;
- if (taskCount == totalCount) {
- success.run();
- }
- public class DispatchGroup {
- private int count = 0;
- private Runnable runnable;
- public DispatchGroup()
- {
- super();
- count = 0;
- }
- public synchronized void enter(){
- count++;
- }
- public synchronized void leave(){
- count--;
- notifyGroup();
- }
- public void notify(Runnable r) {
- runnable = r;
- notifyGroup();
- }
- private void notifyGroup(){
- if (count <=0 && runnable!=null) {
- runnable.run();
- }
- }
- }
- public void onResponse(JSONArray response) {
- try {
- final int[] taskFinished = {0};
- final int taskTotal = response.length();
- for (int i = 0; i < response.length(); i++) {
- JSONObject comment = response.getJSONObject(i);
- outComments.add(comment);
- outUserIds.addAll(helperParseRelaventUserIdsFromEntity(comment));
- outReplies.add(new ArrayList<JSONObject>());
- //TODO: DISPATCH_GROUP?
- String id = comment.getString("_id");
- HashMap<String, String> replyParams = new HashMap<>();
- replyParams.put(API.article.commentId, id);
- final int finalI = i;
- JsonArrayRequest replyRequest = new JsonArrayRequest(Request.Method.GET, API.baseUrl + API.article.listCreateReply, new JSONObject(replyParams), new Response.Listener<JSONArray>() {
- @Override
- public void onResponse(JSONArray response) {
- taskFinished[0]++;
- try {
- for (int j = 0; j < response.length(); j++) {
- JSONObject reply = response.getJSONObject(j);
- outUserIds.addAll(helperParseRelaventUserIdsFromEntity(reply));
- outReplies.get(finalI).add(reply);
- }
- } catch (JSONException ex) {}
- if (taskFinished[0] == taskTotal) {
- success.run();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- taskFinished[0]++;
- if (taskFinished[0] == taskTotal) {
- success.run();
- }
- }
- });
- queue.add(replyRequest);
- }
- } catch (JSONException ex) {}
- }
- task {
- //some (long running) operation, or just:
- 1 + 1
- } then {
- i -> "result: $i"
- } success {
- msg -> println(msg)
- }
- val dispatchGroup = DispatchGroup()
- dispatchGroup.enter()
- // Some long running task
- dispatchGroup.leave()
- dispatchGroup.notify {
- // Some code to run after all dispatch groups complete
- }
- class DispatchGroup {
- private var count = 0
- private var runnable: (() -> Unit)? = null
- init {
- count = 0
- }
- @Synchronized
- fun enter() {
- count++
- }
- @Synchronized
- fun leave() {
- count--
- notifyGroup()
- }
- fun notify(r: () -> Unit) {
- runnable = r
- notifyGroup()
- }
- private fun notifyGroup() {
- if (count <= 0 && runnable != null) {
- runnable!!()
- }
- }
- }
Add Comment
Please, Sign In to add comment