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

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

问题描述

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



我们知道没有明确的答案,但我们无法找到关于这种具体情况的答案。我们阅读了这篇文章:何时使用客户端或服务器端?
,但这不完全一样。



我声称我们应尽量从客户端和数据库,并且让这些查询由负责(服务器)的人运行,我的朋友声称这增加了不必要的复杂性,因为使用Parse提供的工具从客户端访问数据库是非常自然的,而不需要协议等。



我们会征求任何意见,



谢谢。

解决方案

一般来说,立即进行正常通话



我鼓励你在任何情况下首先,让一切正常工作。



< 如果您要执行多个平台 即iOS和Android),云代码可以是 巨大的时间



>不要忘记,对于简单的调用,云代码是浪费时间。 正常解析调用令人惊讶,令人难以置信,惊人,快速和快速的工作。



使用正常的解析调用绝对没有什么错误 。



关于这个问题,你什么时候必须使用云代码调用 - 你会知道,因为你不能用一个正常调用:)



不要忘记,你经常可以在云代码中使用afterSave或beforeSave,做大量的工作。



这是一个奇妙的

h1>解析云代码的经验法则--------->

如果你必须做多个事情... case你可能必须使它成为一个云代码函数。 如果你必须做三个或更多的事情,那么DEFINITELY使它成为云代码函数。



一个好的经验法则。



(再次,正如我所说,通常只是一个afterSave或类似的作品出色地...而不是直接写一个完整的自定义调用。 )



这是一个典型的云计算呼叫,可以在dotcom覆盖的所有平台上节省180亿行代码。首先是云代码...

  Parse.Cloud.define(clientRequestHandleInvite,function(request,response)
{
//从客户端调用,接受来自invitorPerson的邀请

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

//模式很可能是接受或忽略

console.log(clientRequestAcceptInvite called ... invitorPersonId + invitorPersonId +由用户:+ thisUserObj.id);
console.log(clientRequestAcceptInvite called ... theMode is+ theMode);

if(invitorPersonId == undefined || invitorPersonId ==)
{
response.error(clientRequestAcceptInvite中的问题,'invitorPersonId'missing或blank?);
return;
}

var query = new Parse.Query(Parse.User);
query.get(
invitorPersonId,
{
success:function(TheInvitorPersonObject)
{
console.log(clientRequestFriendRemove ... internal)我得到了userObj ...('no response'mode));

if(theMode ==accept)
{
createOneNewHaf(thisUserObj,theInvitorPersonObject);
createOneNewHaf(TheInvitorPersonObject,thisUserObj);
}

//在accept或ignore两种情况下,删除有问题的邀请:
//,除此之外,你必须这样做

deleteFromInvites(TheInvitorPersonObject,thisUserObj);
deleteFromInvites(thisUserObj,theInvitorPersonObject);

//(这些进一步的功能存在于云代码中)

//现在我们只需要使用LETTING THOSE RUN
//所以不要这个........... response.success(正在移除尝试中);
//这是Parse的一个很大的问题(到目前为止,2014)处理不好:
//读取:
// parse.com/questions/can-i-use- a-cloud-code-function-within-another-cloud-code-function
},
错误:function(object,error)
{
console.log(clientRequestAcceptInvite ...内部异常失败:+ error.code ++ error.message);
response.error(Problem,internal problem?);
return;
}
}
);

}
);

如果你是Parse的新手,那么难以想象如何从Android或iOS调用它们!这是一个从Android ...



这将节省你一天的麻烦关于HashMaps:)

  private static void handleInvite(ParseUser invitor,final boolean accepted)
{
String invitorId = invitor.getObjectId();
//您必须向云代码发送ID,而不是PARSEUSER对象。吮吸!

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();
//在返回时处理异常...
}
});

}

现在,直到你必须在SWIFT中这样做

   - (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
int thisRow = indexPath.row;
PFUser * delFriend = [self.theFriends objectAtIndex:thisRow];

NSLog(@您要删除..%@,[delFriend fullName]);

//注意,这个云调用是愉快的设置和忘记
//没有任何方式返回。生活有时像

[PFCloud callFunctionInBackground:@clientRequestFriendRemove
withParameters:@ {
@removeThisFriendId:delFriend.objectId
}
:^(NSString * serverResult,NSError * error)
{
if(!error)
{
NSLog(@ok,Return(string)%@,serverResult) ;
}
}];

[self back]; // that simple
}

注意对于iOS / Swift体验,点击:如何使此Parse.com云代码调用?,其中包括Parse.com团队的意见。希望它节省一些人打字,欢呼


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.

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,

Thank you.

解决方案

In general, go right ahead and make a normal call.

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

Then if necessary go to Cloud Code.

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

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.

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

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.

Here's a fantastic

Rule of thumb for Parse 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.

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

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

}
);

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 ...

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

    }

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
    }

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天全站免登陆