简体   繁体   English

Android Google Play / Drive Api

[英]Android Google Play / Drive Api

Hi I'm using the Google Drive Api to store a database using the AppDataFolder facility. 您好我正在使用Google Drive Api使用AppDataFolder工具存储数据库。 I have a test app running successfully on one device. 我有一个测试应用程序在一台设备上成功运行。 I am able to upload/update/delete/download the database file and reintegrate it into the program with no problems. 我能够上传/更新/删除/下载数据库文件,并将其重新集成到程序中,没有任何问题。

The issue I'm having is when I want to share this database with another device running the same app, then things don't work as expected. 我遇到的问题是,当我想与运行相同应用程序的其他设备共享此数据库时,事情无法按预期工作。 Example device AI upload the database, device B - I want to download the database but no file is found (this delay can vary greatly from seconds to hours). 示例设备AI上传数据库,设备B - 我想下载数据库但没有找到文件(这种延迟可能在几秒到几小时之间变化很大)。 Reason for this - when using the Api it decides when it wants to 'sync' data, as it is queued rather than being instantaneously uploaded. 原因 - 当使用Api时,它决定何时“同步”数据,因为它排队而不是即时上传。 So when used on one device this is not a problem because it takes either the 'synced' file from the cloud storage, or file waiting to be synced. 因此,当在一台设备上使用时,这不是问题,因为它需要来自云存储的“同步”文件或等待同步的文件。

I have tried various things like trying to list all AppDataFolder files or retrieving metadata through a query with searchable filters before making a request to update/delete etc.. However I can't get it to work as desired fundamentally it chooses when to sync. 我尝试过各种各样的事情,例如尝试列出所有AppDataFolder文件或通过带有可搜索过滤器的查询检索元数据,然后再发出请求更新/删除等等。但是,我无法让它根据需要从根本上选择何时同步。

So my actual question is: How can I force Google Drive to sync my file when I want it to ie every time a request is made, so that synchronisation is achieved across multiple devices using the same app. 所以我的实际问题是:如何在我需要时强制Google Drive同步我的文件,即每次发出请求时,都可以使用同一个应用程序在多个设备上实现同步。 There must be an answer as I would think this is quite a fundamental reason why you would use the AppDataFolder is the first place. 必须有一个答案,因为我认为这是你使用AppDataFolder的第一个根本原因。

Thanks in advance 提前致谢

EDIT/UPDATE: 编辑/ UPDATE:

I have been able to find an option in the Api to 'sync' the drive content using this code: 我已经能够在Api中找到一个选项,使用以下代码“同步”驱动器内容:

            // try to sync
            Drive.DriveApi.requestSync(mGoogleApiClient).setResultCallback(new ResultCallback<com.google.android.gms.common.api.Status>() {
                @Override
                public void onResult(com.google.android.gms.common.api.Status status) {
                    if (!status.getStatus().isSuccess()) {
                        Log.e("SYNCING", "ERROR" + status.getStatusMessage());
                    } else {
                        Log.e("SYNCING", "SUCCESS");
                        // execute async task to list AppFolderContents
                        new AppFolderContentsAsyncTask(getActivity()).execute();
                    }
                }
            });

This works well for 3/4 attempts in quick succession, however I reach a syncing limit and status message: 这适用于快速连续3/4次尝试,但是我达到了同步限制和状态消息:

ERRORSync request rate limit exceeded. 超出了ERRORSync请求速率限制。

Is there any way to increase the request rate as this is not really desirable if I have to have a app that prompts the user 'please try again later to sync - not sure how long you'll have to wait until you can though!' 有没有办法提高请求率,因为如果我必须有一个提示用户的应用程序'请稍后再试一次同步 - 不知道你需要多久才能等到你可以!

SOLUTION - OF SORTS, AND MY THOUGHTS (for what its worth) 解决方案 - 对于港口和我的想法(其价值)

The solution that I am going for (after emailing a app dev whose published a drive app that synchronizes without problems) is to use the Drive REST Api, rather than the newer (and Google preferred) Drive API. 我正在寻找的解决方案(通过电子邮件发送一个发布了同步而没有问题的驱动器应用程序的应用程序开发者)是使用Drive REST Api,而不是更新(和Google首选)的Drive API。 I tried limiting the 'requestSync' to only when the user navigated to a fragment with the Drive options (rather than every file transaction). 我尝试将'requestSync'限制为仅当用户导航到带有Drive选项的片段(而不是每个文件事务)时。 However this would probably solve the requestSync rate limit for the most part, but it still could hit that limit. 然而,这可能会解决大多数情况下的requestSync速率限制,但它仍然可以达到该限制。 Also if multiple devices are running the app, and linked to the same Drive account were both syncing/uploading/deleting files at the same time in the app then there is a possibility of losing synchronization - maybe a rare scenario, but still possible. 此外,如果多个设备正在运行应用程序,并且链接到同一个驱动器帐户同时在应用程序中同步/上传/删除文件,则可能会失去同步 - 可能是一种罕见的情况,但仍然可能。 I don't feel that making a user wait to sync files is a viable option in terms of user experience or app design. 在用户体验或应用程序设计方面,我不认为让用户等待同步文件是一个可行的选择。

Curious though - The actual Drive app lets you refresh (requestSync?) as many times as you like. 但好奇 - 实际的Drive应用程序允许您根据需要多次刷新(requestSync?)。 I created 20 folders on the web interface in quick succession, after each folder was created I refreshed the Drive app on my phone and it synced all 20 times. 我在网络界面上快速连续创建了20个文件夹,在创建每个文件夹后,我刷新了手机上的云端硬盘应用程序并同步了20次。 It seems Google understands the importance of synchronization, but chooses to make this quite difficult ensure this in their new Drive API. 谷歌似乎理解同步的重要性,但选择在新的Drive API中确保这一点非常困难。 As already stated uploading files to the cloud storage happens usually instantly (it is queued, however if you have connectivity it happens almost straight away). 如前所述,将文件上传到云存储通常会立即发生(它排队,但如果你有连接,它几乎会立即发生)。 I would have thought that this is the costly transaction in terms of moving data/updating drive contents, rather than just querying for synchronization of files. 我认为这是移动数据/更新驱动器内容方面的代价高昂的交易,而不仅仅是查询文件的同步。 However you can keep adding files to your your drive app one at a time, and it uploads them one at a time almost instantly - where as you request sync more than 4 times in 30 seconds and it then fails and you have to 'cool off' for a while. 但是,您可以一次一个地向您的驱动器应用程序添加文件,并且几乎立即上传一个文件 - 在30秒内请求同步超过4次然后失败并且您必须'冷静下来' 一阵子。

I'm very new to programming/Android in general - as soon as I learn something new, I realize how little I actually know, so if their is a better solution out there using the Drive API (rather than REST API) I'd very much welcome it. 我对编程/ Android一般都很陌生 - 只要我学到新东西,我就会意识到我实际上并不知道,所以如果他们使用Drive API(而不是REST API)是一个更好的解决方案我会非常欢迎它。

DriveApi#requestSync will let you request a "sync down" from the server to the device. DriveApi#requestSync将允许您请求从服务器到设备的“同步”。 This way, on the second device you should be able to see the content uploaded from the first device. 这样,在第二台设备上,您应该能够看到从第一台设备上传的内容。 However, the calls to request sync are rate limited (per-device) to avoid abuse and guarantee a reasonable amount of data usage by the Drive API. 但是,对请求同步的调用是速率限制的(每个设备),以避免滥用并保证Drive API使用合理数量的数据。 You should ideally call request sync only when you need to. 理想情况下,您应该只在需要时调用请求同步。 There's no way to increase the rate limit. 没有办法提高速度限制。

Regarding upload completion, after committing an upload, you shouldn't use request sync since that won't help you (it only syncs down but not up). 关于上传完成,在提交上传后,您不应该使用请求同步,因为这对您没有帮助(它只会同步但不能同步)。 The actual upload will happen as soon as possible (based on the device's connectivity and the preferences specified by your app through DrivePreferencesApi , which by default are unrestricted). 实际上传将尽快发生(基于设备的连接性和应用程序通过DrivePreferencesApi指定的首选项,默认情况下不受限制)。

If you want to know when the actual upload happened, you can use CompletionEvents . 如果您想知道实际上传的时间,可以使用CompletionEvents This way you can test your app with more insights on what's actually going on. 通过这种方式,您可以通过对实际情况的更多见解来测试您的应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM