Skip to main content

Overview

TournamentFetcher retrieves the list of tournaments in which the current logged-in user is a participant. It requires both a valid AuthenticationToken with the gaming graph domain and a valid AccessToken. TournamentUpdater is a companion class that posts a new score to a specific tournament. Module: FacebookGamingServices
Declared in: TournamentFetcher.swift, TournamentUpdater.swift

TournamentFetcher

Initializer

public convenience init()
Creates a TournamentFetcher with the default GraphRequestFactory. You typically use this initializer directly.

Methods

fetchTournaments(completionHandler:)

Fetches all tournaments where the current user is a participant.
public func fetchTournaments(
    completionHandler: @escaping (Result<[Tournament], TournamentFetchError>) -> Void
)
completionHandler
(Result<[Tournament], TournamentFetchError>) -> Void
A closure called on completion. On success, receives an array of Tournament values. On failure, receives a TournamentFetchError.
Preconditions:
  • AuthenticationToken.current must be non-nil and have graphDomain == "gaming".
  • AccessToken.current must be non-nil.
If either condition is not met, the completion is called immediately with .failure(.invalidAuthToken) or .failure(.invalidAccessToken) respectively.

TournamentFetchError enum

CaseDescription
.server(Error)The Graph API returned an error. The associated value is the underlying error.
.decodingThe response could not be decoded into [Tournament].
.invalidAuthTokenAuthenticationToken.current is nil or not in the gaming domain.
.invalidAccessTokenAccessToken.current is nil.

Tournament model

Tournament is a Codable struct that represents a single tournament.

Properties

identifier
String
The unique identifier for this tournament. Corresponds to the id field in the Graph API response.
endTime
Date?
The timestamp when the tournament ends. If this date is in the past, the tournament has expired.
title
String?
The display title of the tournament as set during creation.
payload
String?
Custom data attached to the tournament during creation. Available to all game sessions launched from tournament updates.

TournamentConfig struct

TournamentConfig is used when creating a new tournament via ShareTournamentDialog. It describes the configuration of the tournament before it is created on the server.

Initializer

public init(
    title: String? = nil,
    endTime: Date? = nil,
    scoreType: TournamentScoreType? = nil,
    sortOrder: TournamentSortOrder? = nil,
    image: UIImage? = nil,
    payload: String? = nil
)
title
String?
Display title for the tournament.
endTime
Date?
The end date of the tournament. Defaults to one week after creation if not specified.
scoreType
TournamentScoreType?
How scores are formatted in the leaderboard. Defaults to TournamentScoreType.numeric.
sortOrder
TournamentSortOrder?
Whether higher or lower scores rank better. Defaults to TournamentSortOrder.descending.
image
UIImage?
An image to associate with the tournament and include in any posts.
payload
String?
Optional custom data string attached to the tournament. Must be 1000 characters or fewer when stringified.

Properties

title
String?
Display title of the tournament.
endTime
TimeInterval?
Stored internally as TimeInterval (Unix timestamp). Converted from Date at initialization.
scoreType
TournamentScoreType?
Score formatting preference.
sortOrder
TournamentSortOrder?
Score ranking order.
image
UIImage?
Image associated with the tournament.
payload
String?
Custom data string.

TournamentUpdater

TournamentUpdater posts a score update to a specific tournament.

Initializer

public convenience init()

Methods

update(tournamentID:score:completionHandler:)

Updates the current user’s score in the specified tournament by ID.
public func update(
    tournamentID: String,
    score: Int,
    completionHandler: @escaping (Result<Bool, TournamentUpdaterError>) -> Void
)
tournamentID
String
The ID of the tournament to update. Must not be empty.
score
Int
The new score to record for the current user in this tournament.
completionHandler
(Result<Bool, TournamentUpdaterError>) -> Void
A closure called on completion. On success, receives true. On failure, receives a TournamentUpdaterError.

update(tournament:score:completionHandler:)

Updates the current user’s score using a Tournament object.
public func update(
    tournament: Tournament,
    score: Int,
    completionHandler: @escaping (Result<Bool, TournamentUpdaterError>) -> Void
)
tournament
Tournament
The tournament to update.
score
Int
The new score.
completionHandler
(Result<Bool, TournamentUpdaterError>) -> Void
A closure called on completion.

TournamentUpdaterError enum

CaseDescription
.server(Error)The Graph API returned an error.
.decodingThe server response could not be decoded.
.invalidAuthTokenAuthenticationToken.current is nil or not in the gaming domain.
.invalidAccessTokenAccessToken.current is nil.
.invalidTournamentIDThe provided tournamentID is an empty string.

Usage example

import FacebookGamingServices

// Fetch tournaments
let fetcher = TournamentFetcher()
fetcher.fetchTournaments { result in
    switch result {
    case let .success(tournaments):
        for tournament in tournaments {
            print("Tournament:", tournament.title ?? "Untitled")
            print("Ends:", tournament.endTime ?? Date.distantFuture)
        }
    case let .failure(error):
        print("Fetch failed:", error)
    }
}

// Update a score
let updater = TournamentUpdater()
updater.update(tournamentID: "12345678", score: 9500) { result in
    switch result {
    case .success:
        print("Score updated")
    case let .failure(error):
        print("Update failed:", error)
    }
}

Build docs developers (and LLMs) love