Android:在使用 Parse 时使用服务器端 [英] Android: Using server-side when working with Parse

查看:25
本文介绍了Android:在使用 Parse 时使用服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的朋友正在开发一个应用程序.我们希望使用 Parse.com 作为我们可以从中检索信息的数据库.我们无法决定访问 Parse 数据的最佳方式是什么.为了这个例子,我们的应用程序.(即客户端)需要在 Parse 数据库中存储一些东西(比如某个数字) - 它应该使用 Parse API 直接运行查询,还是应该向服务器端发出请求,让它从 Parse 检索该数字,以及寄回给客户?

Me and my friend are working on an app., and we wish to use Parse.com as our data base from which we can retrieve info. We can't decide what is the best way to access the data on Parse. For the sake of the example, our app. (i.e. client side) needs something stored on the Parse data base (say some number) - should it directly run the query using the Parse API, or should it make a request to a server side, let it retrieve that number from Parse, and send it back to the client?

我们知道没有明确的答案,但我们无法找到有关这种特定情况的答案.我们阅读了这篇文章:何时使用客户端或服务器端?,但这并不完全相同.

We know there's no definite answer, but we couldn't find answer regarding this specific situation. We read this post: When to use client-side or server-side?, but this not exactly the same case.

我声称我们应该尽量将客户端和数据库分开,让这些查询由负责人(服务器)运行,我的朋友声称这会增加不必要的复杂性,因为很自然地使用Parse提供的工具从客户端访问数据库,不需要协议等.

I claim that we should try to seperate as much as possible from client side and data bases, and leave these queries run by someone who's in charge (server), where my friend claims this adds unnecessary complication, since it's very natural to use the tools supplied by Parse to access the data base from the client side, without the need for a protocol etc.

我们愿意提供任何建议,

We'd appriciate any advice,

谢谢.

推荐答案

一般来说,直接拨打普通电话.

无论如何,我都鼓励您首先这样做,以使一切都在两端都能正常工作.

I'd encourage you to do that first in any case, to get everything working on both ends.

然后如有必要,请转到 Cloud Code.

Then if necessary go to Cloud Code.

如果您要多个平台(即 iOS 和 Android),云代码可以节省大量时间.

If you are going to do more than one platform (ie iOS and Android), cloud code can be a huge timesaver.

但是不要忘记,对于简单的调用,云代码是浪费时间.正常"Parse 调用非常、令人难以置信、令人惊讶、快速且易于使用.

BUT don't forget that for simple calls, cloud code is a waste of time. "Normal" Parse calls are amazingly, incredibly, amazingly, fast and quick to work with.

绝对没有错误";使用普通的 Parse 调用 - 这样做.

There is absolutely nothing "wrong" with using normal Parse calls - so do that.

关于这个问题,你什么时候必须使用云代码调用——你会知道的,因为你将无法通过普通调用来实现:)

Regarding the question, when do you literally have to use a cloud code call -- you'll know, because you won't be able to do it with a normal call :)

不要经常忘记,您可以简单地使用afterSave";或保存前"在云代码中,要做大量的工作.您通常实际上并不需要进行自定义呼叫".在云代码中.

Don't forget very often you can simply use "afterSave" or "beforeSave" in cloud code, to do a huge amount of work. You often don't literally need to go to a "custom call" in cloud code.

这里有一个很棒的

如果您必须做不止一件事"...在这种情况下,您可能必须将其设为云代码功能.如果你必须做三件事或更多事"然后绝对让它成为一个云代码功能.

If you have to do "more than one thing" ... in that case you will likely have to make it a cloud code function. If you have to do "three or more things" then DEFINITELY make it a cloud code function.

这是一个很好的经验法则.

That's a good rule of thumb.

(同样,正如我所说,通常只是一个afterSave"或类似的工作出色......而不是字面上写一个完整的自定义调用.)

(Again, as I say, often just an "afterSave" or similar works brilliantly...rather than literally writing a full custom call.)

这是一个典型的云调用示例,它在 dotcom 覆盖的所有平台中节省了 180 亿行代码.先上云代码...

Here's a typical example of a cloud call that saves 18 billion lines of code in all the platforms covered by the dotcom. First the cloud code...

Parse.Cloud.define("clientRequestHandleInvite", function(request, response)
{
// called from the client, to accept an invite from invitorPerson

var thisUserObj = request.user;
var invitorPersonId = request.params.invitorPersonId;
var theMode = request.params.theMode;

// theMode is likely "accept" or "ignore"

console.log( "clientRequestAcceptInvite called....  invitorPersonId " + invitorPersonId + " By user: " + thisUserObj.id );
console.log( "clientRequestAcceptInvite called....  theMode is " + theMode );

if ( invitorPersonId == undefined || invitorPersonId == "" )
  {
  response.error("Problem in clientRequestAcceptInvite, 'invitorPersonId' missing or blank?");
  return;
  }

var query = new Parse.Query(Parse.User);
query.get(
  invitorPersonId,
    {
    success: function(theInvitorPersonObject)
      {
      console.log("clientRequestFriendRemove ... internal I got the userObj ...('no response' mode)");
      
      if ( theMode == "accept" )
        {
        createOneNewHaf( thisUserObj, theInvitorPersonObject );
        createOneNewHaf( theInvitorPersonObject, thisUserObj );
        }
      
      // in both cases "accept" or "ignore", delete the invite in question:
      // and on top of that you have to do it both ways
      
      deleteFromInvites( theInvitorPersonObject, thisUserObj );
      deleteFromInvites( thisUserObj, theInvitorPersonObject );
      
      // (those further functions exist in the cloud code)
      
      // for now we'll just go with the trick of LETTING THOSE RUN
      // so DO NOT this ........... response.success( "removal attempt underway" );
      // it's a huge problem with Parse that (so far, 2014) is poorly handled:
      // READ THIS:
      // parse.com/questions/can-i-use-a-cloud-code-function-within-another-cloud-code-function
      },
    error: function(object,error)
      {
      console.log("clientRequestAcceptInvite ... internal unusual failure: " + error.code + " " + error.message);
      response.error("Problem, internal problem?");
      return;
      }
    }
  );

}
);

如果您是 Parse 的新手,很难弄清楚如何从 Android 或 iOS 调用这些!这是从 Android 调用的那个...

If you are new to Parse it's incredibly hard to figure out how to call these from Android or iOS! Here's that one being called from Android ...

这将节省你一天的时间来处理 HashMap :)

this will save you a day of messing about with HashMaps :)

private static void handleInvite( ParseUser invitor, final boolean accepted )
    {
    String invitorId = invitor.getObjectId();
    // you must SEND IDs, NOT PARSEUSER OBJECTS to cloud code. Sucks!

    String cloudKode;
    cloudKode = (accepted? "accept" : "ignore");

    HashMap<String, Object> dict = new HashMap<String, Object>();
    dict.put( "invitorPersonId", invitorId );
    dict.put( "theMode", cloudKode );

    Toast.makeText(State.mainContext, "contacting...", Toast.LENGTH_SHORT).show();

    ParseCloud.callFunctionInBackground(
        "clientRequestHandleInvite",
         dict,
         new FunctionCallback<Object>()
    {
    @Override
    public void done(Object s, ParseException e)
        {
        Toast.makeText(State.mainContext, "blah", Toast.LENGTH_SHORT).show();
        // be careful with handling the exception on return...
        }
    });

    }

这是来自 iOS 的相同云调用......现在很好,直到您必须在 SWIFT 中进行

And here's the same cloud call from iOS ... well for now, until you have to do it in SWIFT

-(void)tableView:(UITableView *)tableView
        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
        forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    int thisRow = indexPath.row;
    PFUser *delFriend = [self.theFriends objectAtIndex:thisRow];
    
    NSLog(@"you wish to delete .. %@", [delFriend fullName] );
    
    // note, this cloud call is happily is set and forget
    // there's no return either way. life's like that sometimes
    
    [PFCloud callFunctionInBackground:@"clientRequestFriendRemove"
            withParameters:@{
                            @"removeThisFriendId":delFriend.objectId
                            }
            block:^(NSString *serverResult, NSError *error)
            {
            if (!error)
                {
                NSLog(@"ok, Return (string) %@", serverResult);
                }
            }];
    
    [self back];    // that simple
    }

注意对于 iOS/Swift 体验,请点击:如何制作这个 Parse.com 云代码呼叫?,其中包括 Parse.com 团队的评论.希望它可以为某人节省一些打字时间,干杯

Note For the iOS/Swift experience, click to: How to make this Parse.com cloud code call? which includes comments from the Parse.com team. Hope it saves someone some typing, cheers

这篇关于Android:在使用 Parse 时使用服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆