Skip to content

Commit eac7301

Browse files
committed
fix(ios): pass inputData to periodic background tasks
- Store inputData in UserDefaults when registering periodic tasks - Retrieve and pass stored inputData when executing periodic tasks - Fix issue where periodic tasks received nil inputData instead of provided data - Update documentation with periodic task inputData examples Closes #612
1 parent 7f4f870 commit eac7301

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

docs/quickstart.mdx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ Workmanager().registerPeriodicTask(
176176
"cleanup",
177177
frequency: Duration(hours: 24),
178178
);
179+
180+
// Schedule a periodic task with input data
181+
Workmanager().registerPeriodicTask(
182+
"sync-task",
183+
"data_sync",
184+
frequency: Duration(hours: 6),
185+
inputData: <String, dynamic>{
186+
'server_url': 'https://api.example.com',
187+
'sync_type': 'full',
188+
'max_retries': 3,
189+
},
190+
);
191+
```
179192
```
180193
181194
## Task Results
@@ -200,4 +213,4 @@ Your background tasks can return:
200213
201214
- **[Task Customization](customization)** - Advanced configuration with constraints, input data, and management
202215
- **[Debugging Guide](debugging)** - Learn how to debug and troubleshoot background tasks
203-
- **[Example App](https://github.com/fluttercommunity/flutter_workmanager/tree/main/example)** - Complete working demo
216+
- **[Example App](https://github.com/fluttercommunity/flutter_workmanager/tree/main/example)** - Complete working demo

workmanager_apple/ios/Sources/workmanager_apple/UserDefaultsHelper.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ struct UserDefaultsHelper {
1515

1616
enum Key {
1717
case callbackHandle
18+
case periodicTaskInputData(taskIdentifier: String)
1819

1920
var stringValue: String {
20-
return "\(WorkmanagerPlugin.identifier).\(self)"
21+
switch self {
22+
case .callbackHandle:
23+
return "\(WorkmanagerPlugin.identifier).callbackHandle"
24+
case .periodicTaskInputData(let taskIdentifier):
25+
return "\(WorkmanagerPlugin.identifier).periodicTaskInputData.\(taskIdentifier)"
26+
}
2127
}
2228
}
2329

@@ -31,6 +37,16 @@ struct UserDefaultsHelper {
3137
return getValue(for: .callbackHandle)
3238
}
3339

40+
// MARK: periodicTaskInputData
41+
42+
static func storePeriodicTaskInputData(_ inputData: [String: Any]?, forTaskIdentifier taskIdentifier: String) {
43+
store(inputData, key: .periodicTaskInputData(taskIdentifier: taskIdentifier))
44+
}
45+
46+
static func getStoredPeriodicTaskInputData(forTaskIdentifier taskIdentifier: String) -> [String: Any]? {
47+
return getValue(for: .periodicTaskInputData(taskIdentifier: taskIdentifier))
48+
}
49+
3450
// MARK: Private helper functions
3551

3652
private static func store<T>(_ value: T, key: Key) {

workmanager_apple/ios/Sources/workmanager_apple/WorkmanagerPlugin.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
4444
// If frequency is not provided it will default to 15 minutes
4545
schedulePeriodicTask(taskIdentifier: task.identifier, earliestBeginInSeconds: earliestBeginInSeconds ?? (15 * 60))
4646

47+
// Retrieve the stored inputData for this periodic task
48+
let storedInputData = UserDefaultsHelper.getStoredPeriodicTaskInputData(forTaskIdentifier: task.identifier)
49+
4750
let operationQueue = OperationQueue()
4851
let operation = createBackgroundOperation(
4952
identifier: task.identifier,
50-
inputData: nil,
53+
inputData: storedInputData,
5154
backgroundMode: .backgroundPeriodicTask(identifier: identifier)
5255
)
5356

@@ -191,6 +194,13 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
191194

192195
executeIfSupportedVoid(completion: completion, feature: "PeriodicTask") {
193196
let initialDelaySeconds = Double(request.initialDelaySeconds ?? 0)
197+
198+
// Store the inputData for later retrieval when the task executes
199+
UserDefaultsHelper.storePeriodicTaskInputData(
200+
request.inputData as? [String: Any],
201+
forTaskIdentifier: request.uniqueName
202+
)
203+
194204
WorkmanagerPlugin.schedulePeriodicTask(
195205
taskIdentifier: request.uniqueName,
196206
earliestBeginInSeconds: initialDelaySeconds

0 commit comments

Comments
 (0)