Skip to content

Commit ef8892b

Browse files
Fixing broken api url for background task.
1 parent 1359c16 commit ef8892b

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

utils/tasks.utils.ts

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,52 @@
1-
import * as TaskManager from "expo-task-manager";
2-
import * as BackgroundFetch from "expo-background-fetch";
1+
import * as TaskManager from 'expo-task-manager';
2+
import * as BackgroundFetch from 'expo-background-fetch';
33

4-
const BACKGROUND_FETCH_TASK = "background-fetch-task";
4+
const BACKGROUND_FETCH_TASK = 'background-fetch-task';
55

66
// Define the task
77
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
8-
try {
9-
console.log("Background fetch task started");
10-
// Perform your API request
11-
const response = await fetch(
12-
"https://new.codebuilder.org/api/reddit/posts"
13-
);
14-
const data = await response.json();
8+
try {
9+
console.log('----------------------- Background fetch task started ---------------------');
10+
// Perform your API request
11+
const response = await fetch('https://new.codebuilder.org/api/jobs');
12+
const contentType = response.headers.get('content-type') || '';
1513

16-
// Handle the fetched data
17-
console.log("Fetched data:", data);
14+
if (!response.ok) {
15+
const text = await response.text();
16+
console.error('Background fetch HTTP error', response.status, text.slice(0, 200));
17+
return BackgroundFetch.BackgroundFetchResult.Failed;
18+
}
1819

19-
return BackgroundFetch.BackgroundFetchResult.NewData; // Task succeeded
20-
} catch (error) {
21-
console.error("Background fetch failed:", error);
22-
return BackgroundFetch.BackgroundFetchResult.Failed; // Task failed
23-
}
20+
let data: unknown;
21+
if (contentType.includes('application/json')) {
22+
data = await response.json();
23+
} else {
24+
const text = await response.text();
25+
console.warn('Background fetch received non-JSON response:', text.slice(0, 200));
26+
data = text;
27+
}
28+
29+
// Handle the fetched data
30+
console.log('Fetched data:', data);
31+
32+
return BackgroundFetch.BackgroundFetchResult.NewData; // Task succeeded
33+
} catch (error) {
34+
console.error('Background fetch failed:', error);
35+
return BackgroundFetch.BackgroundFetchResult.Failed; // Task failed
36+
}
2437
});
2538

2639
// Register the task
2740
export async function registerBackgroundFetch() {
28-
const status = await BackgroundFetch.getStatusAsync();
29-
if (status === BackgroundFetch.BackgroundFetchStatus.Available) {
30-
await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
31-
minimumInterval: 60, // Fetch interval in seconds (not guaranteed to be exact)
32-
stopOnTerminate: false, // Continue task when app is closed
33-
startOnBoot: true, // Start task when device is rebooted
34-
});
35-
console.log("Background fetch task registered");
36-
} else {
37-
console.error("Background fetch is not available");
38-
}
41+
const status = await BackgroundFetch.getStatusAsync();
42+
if (status === BackgroundFetch.BackgroundFetchStatus.Available) {
43+
await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
44+
minimumInterval: 60, // Fetch interval in seconds (not guaranteed to be exact)
45+
stopOnTerminate: false, // Continue task when app is closed
46+
startOnBoot: true, // Start task when device is rebooted
47+
});
48+
console.log('Background fetch task registered');
49+
} else {
50+
console.error('Background fetch is not available');
51+
}
3952
}

0 commit comments

Comments
 (0)