SyncFriendsAsync
Methods
public FriendsOperation<FriendsSyncResult> SyncFriendsAsync(string source, IReadOnlyList<string> friendIds, CancellationToken cancellationToken = default)
Uploads the player's current friends from a given platform.
Parameters
| Type | Name | Description |
|---|---|---|
| string | source | The platform the ids come from, such as |
| IReadOnlyList<string> | friendIds | The friends' ids on that platform, as the platform gives them to you. coherence Cloud currently accepts up to 2000 ids, each up to 64 characters long. |
| CancellationToken | cancellationToken | Cancels the request. |
Returns
| Type | Description |
|---|---|
| FriendsOperation<FriendsSyncResult> | How many of the uploaded friends play your game, and how many were dropped. |
Remarks
Syncing the friends list through this method replaces whatever was synced before.
Call this method only after logging in, and again whenever the platform tells you the friend list changed. Each platform is synced on its own, so for example, syncing Steam never disturbs friends synced from Game Center.
Passing an empty list is how you remove every friend synced from that platform, which you can do when the player revokes access to their friend list. If you could not read the list at all, do not sync through this method; leave the friends already synced from it alone.
The friend list is refreshed before the returned request finishes, so Friends is up to date by then.
The request fails without a source, with a null
friendIds, or when coherence Cloud turns it down:
Coherence.Runtime.ErrorCode.FriendsRateLimited for too many syncs in a row (currently over 10 a minute),
Coherence.Runtime.ErrorCode.FriendsUnsupportedSource for a platform it does not accept,
or Coherence.Runtime.ErrorCode.PlayerSessionRequired when no player is logged in.
It also fails when the answer coherence Cloud sends cannot be read.
Check HasFailed or use OnFail to see which.
Examples
using Coherence.Cloud;
using UnityEngine;
class FriendsSync : MonoBehaviour
{
async void Start()
{
// Wait until a player account has logged in to coherence Cloud.
// The CoherenceCloudLogin component can be used for this.
// Guest and username/password accounts cannot have friends.
PlayerAccount playerAccount = await PlayerAccount.GetMainAsync();
var friends = playerAccount.Services.Friends;
// Read the player's friend list from wherever you keep it yourself.
// Here we're using placeholder ids, coming from the game's own backend.
var myFriends = new[] { "player-1234567890", "player-9876543210" };
await friends.SyncFriendsAsync("custom", myFriends)
.OnFail(Debug.LogWarning)
.OnSuccess(result => Debug.Log($"{result.Resolved} of them play this game."));
// A friend shows up only once both players have synced each other.
foreach (var friend in friends.Friends)
{
Debug.Log(friend.DisplayName);
}
}
}
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | This service has been disposed. Thrown at the call site rather than reported through the returned request. |