Result
Encapsulates the result of an asynchronous operation, providing a type-safe way to handle both success and failure cases.
This class follows the Result pattern (also known as Either or Try pattern) to represent operations that can succeed with data or fail with an exception. It ensures null-safety by guaranteeing that exactly one of the two fields (data or exception) will be non-null, never both or neither.
Key characteristics:
- Type-safe: Success data type (T) and error type (E) are explicitly defined
- Null-safe: Either data or exception is non-null, but never both
- Immutable: Once created, the result cannot be changed
- Thread-safe: Can be safely passed between threads
Usage pattern: Always check isSuccess first to determine if the operation succeeded, then access either getData for successful results or getException for errors. Basic success/failure handling:
Pushwoosh.getInstance().registerForPushNotifications((result) -> {
if (result.isSuccess()) {
// Safe to access data
RegisterForPushNotificationsResultData data = result.getData();
String token = data.getToken();
Log.d("App", "Registered successfully. Token: " + token);
} else {
// Safe to access exception
RegisterForPushNotificationsException exception = result.getException();
Log.e("App", "Registration failed: " + exception.getMessage());
}
});
// Some operations like setTags() return Void on success
Pushwoosh.getInstance().setTags(tags, (result) -> {
if (result.isSuccess()) {
// result.getData() is null for Void operations
Log.d("App", "Tags set successfully");
} else {
PushwooshException exception = result.getException();
Log.e("App", "Failed to set tags: " + exception.getMessage());
}
});
Pushwoosh.getInstance().registerForPushNotifications((result) -> {
if (result.isSuccess()) {
String token = result.getData().getToken();
saveTokenToPreferences(token);
} else {
Exception exception = result.getException();
// Log full exception with stack trace
Log.e("App", "Registration failed", exception);
// Handle specific error types
if (exception instanceof NetworkException) {
showToast("Network error. Please check your connection.");
} else if (exception.getMessage().contains("Play Services")) {
showPlayServicesUpdateDialog();
} else {
showToast("Registration failed: " + exception.getMessage());
}
}
});
Pushwoosh.getInstance().getTags((result) -> {
if (result.isSuccess()) {
TagsBundle tags = result.getData(); // Never null on success
// Safe access with default values
String name = tags.getString("Name", "Guest");
int age = tags.getInt("Age", 0);
List<String> interests = tags.getList("Interests", Collections.emptyList());
Log.d("App", "User: " + name + ", Age: " + age);
Log.d("App", "Interests: " + interests);
} else {
Log.e("App", "Failed to retrieve tags", result.getException());
}
});
Pushwoosh.getInstance().setUserId("user_12345", (result) -> {
if (result.isSuccess()) {
// Proceed with app initialization
loadUserProfile();
syncUserData();
enablePushFeatures();
} else {
// Fallback to anonymous mode
Log.w("App", "User ID not set, using anonymous mode", result.getException());
enableAnonymousMode();
}
});
Pushwoosh.getInstance().registerForPushNotifications((result) -> {
if (result.isSuccess()) {
RegisterForPushNotificationsResultData data = result.getData();
Log.d("App", "Push registration successful");
Log.d("App", " Token: " + data.getToken());
Log.d("App", " Notifications enabled: " + data.isEnabled());
// Send to analytics
Analytics.logEvent("push_registered", "token", data.getToken());
} else {
Exception exception = result.getException();
// Log with exception for full stack trace
Log.e("App", "Push registration failed: " + exception.getMessage(), exception);
// Send to crash reporting
Crashlytics.recordException(exception);
}
});
Parameters
the type of data returned on successful operation completion
the type of exception returned on operation failure, must extend PushwooshException