我和我的朋友正在开发一个应用程序。我们希望使用Parse.com作为我们的数据库,从中可以检索信息。我们无法决定什么是在Parse上访问数据的最佳方法。为了这个例子,我们的应用程序。(即客户端)需要一些存储在Parse数据库中的内容(例如一些数字)-是使用Parse API直接运行查询,还是向服务器端发出请求,让其从Parse中检索该数字,寄回给客户?
我们知道没有确切的答案,但是找不到关于此特定情况的答案。我们读这篇文章:什么时候使用客户端或服务器端?,但情况并非完全相同。
我声称我们应该尽量将其与客户端和数据库分开,并让这些查询由负责人(服务器)运行,我的朋友声称这会增加不必要的复杂性,因为使用工具非常自然由Parse提供以从客户端访问数据库,而无需协议等。
我们会提供任何建议,
谢谢你。
一般而言,请继续进行并正常拨打电话。
我会鼓励你这样做首先在任何情况下,让两端的一切工作。
然后,如有必要,请转到“云代码”。
如果您打算使用多个平台(即iOS和Android),则云代码可能会节省大量时间。
但是请不要忘记,对于简单的调用,云代码是浪费时间。“正常”解析调用的使用非常惊人,令人难以置信,令人惊奇,快速。
使用普通的Parse调用绝对没有“错误”-这样做。
关于这个问题,您什么时候真正必须使用云代码调用-您会知道的,因为您将无法通过正常的调用来完成它:)
别经常忘记,您可以简单地在云代码中使用“ afterSave”或“ beforeSave”来进行大量工作。通常,您实际上并不需要在云代码中进行“自定义调用”。
这真是太棒了
如果您必须做“一件以上的事情”,那么在这种情况下,您可能必须将其设置为云代码功能。如果您必须做“三件或更多事情”,则一定要使其成为云代码功能。
这是一个很好的经验法则。
(再次,正如我所说,通常只是“ afterSave”或类似的作品很出色……而不是从字面上编写一个完整的自定义调用。)
这是云电话的一个典型示例,在网络覆盖的所有平台中节省了180亿行代码。首先是云代码...
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调用的人...
这将为您节省有关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中完成它为止
-(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团队的评论。希望它可以节省一些打字,喝彩的时间
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句