Batch requests let you send up to 50 Graph API calls in a single HTTP round trip. This reduces latency and is particularly useful when you need related data at the same time — for example, fetching a user’s profile and their friends list simultaneously.
Under the hood, the SDK serialises all requests into a JSON batch body and sends them to the Graph API batch endpoint as a single POST.
Your app ID must be configured in Settings.shared.appID before using batch requests. The SDK throws an NSInternalInconsistencyException if appID is not set.
Creating a connection
Instantiate a GraphRequestConnection, add your requests, then call start().
import FacebookCore
// 1. Create the connection
let connection = GraphRequestConnection()
// 2. Add the first request
let profileRequest = GraphRequest(
graphPath: "me",
parameters: ["fields": "id,name"]
)
connection.add(profileRequest) { _, result, error in
if let result = result as? [String: Any] {
print("Profile:", result)
}
}
// 3. Add the second request
let friendsRequest = GraphRequest(
graphPath: "me/friends",
parameters: ["fields": "id,name"]
)
connection.add(friendsRequest) { _, result, error in
if let result = result as? [String: Any] {
print("Friends:", result)
}
}
// 4. Fire all requests
connection.start()
Adding requests
GraphRequestConnection exposes two variants of add(_:completion:):
// Without a name (anonymous request)
connection.add(request) { connection, result, error in
// handle result
}
// With a name — allows later requests in the same batch to reference
// the result of this request using JSONPath syntax
connection.add(request, name: "profileRequest") { connection, result, error in
// handle result
}
Naming requests is useful when a later request depends on the result of an earlier one in the same batch.
Handling completions
Each request’s completion block is called individually as results arrive. If one request fails, the others still complete. A network-level error is reported separately through the connection’s delegate:
connection.add(profileRequest) { _, result, error in
if let error = error {
print("Profile request failed:", error.localizedDescription)
return
}
guard let profile = result as? [String: Any] else { return }
print("Name:", profile["name"] ?? "")
}
Cancelling a connection
Call cancel() at any point before or during execution to abort all pending requests:
After cancellation the connection transitions to the cancelled state and cannot be restarted.
Configuring the timeout
The default timeout is 60 seconds. You can change it per-connection or globally:
// Per-connection
connection.timeout = 30
// Global default
GraphRequestConnection.setDefaultConnectionTimeout(30)
Complete example
import FacebookCore
func fetchUserData() {
let connection = GraphRequestConnection()
let meRequest = GraphRequest(graphPath: "me", parameters: ["fields": "id,name,email"])
connection.add(meRequest) { _, result, error in
guard error == nil, let user = result as? [String: Any] else { return }
print("User:", user)
}
let pagesRequest = GraphRequest(graphPath: "me/accounts", parameters: ["fields": "id,name"])
connection.add(pagesRequest) { _, result, error in
guard error == nil, let pages = result as? [String: Any] else { return }
print("Pages:", pages)
}
connection.start()
}