Contentful logo

Contentful Community

Use the Java Sync Api in combination with FetchQuery

Hi,
is there a way to use the SyncQuery (to get only updated content) in combination with the FetchQuery
to do something like this .
CDAEntry course = ((CDAEntry) client
.fetch(CDAEntry.class)
.withContentType(“pxc-news”)

If you are looking for the initial sync, this snippet should do it:

// Create the Contentful client.
final CDAClient client =
    CDAClient
        .builder()
        .setToken("<access_token>") // required
        .setSpace("<space_id>") // required
        .setEnvironment("<environment_id>") // optional, defaults to `master`
        .build();

// Synchronization of a specific type is done manually.

// Fetch the synchronized space.
final SynchronizedSpace synchronizedSpace =
    client
        .sync(SyncType.onlyEntriesOfType("<content_type_id>"))
        .fetch();
final String syncToken = synchronizedSpace.nextSyncUrl();
final Map<String, CDAEntry> entries = synchronizedSpace.entries();

Subsequent syncs will only return the content type specified in the initial sync, so you just pass in the sync token returned in the initial sync like so:

// Create the Contentful client.
final CDAClient client =
    CDAClient
        .builder()
        .setToken("<access_token>") // required
        .setSpace("<space_id>") // required
        .setEnvironment("<environment_id>") // optional, defaults to `master`
        .build();

String syncToken = "sync token from previous sync"
// In java, you don't need to know about paging, requesting a sync, will always 
// walk through all pages in a request.
final SynchronizedSpace synchronizedSpace =
    client
        .sync(syncToken)
        .fetch();

I believe that should do it, but I’m not a Java expert so there might be something I’ve missed…

1 Like

Hello everyone,

while Charlie is right in how to use sync in Java, I am still struggling in understanding how you would like to combine sync and fetch.

Can you help me understanding on what you want to achieve?

Thanks,
Mario

1 Like

Hello Charlie, Hello Mario

thank you for the reply. My client must be able to sync the whole content but separated by language.
Thanks
Matthias

Hey,

Thanks for the explanation of the use case. Sadly our API and therefore the SDKs do not support that.

I would recommend you to parse the returned entries from the SynchronizedSpace, and create new models on the client side.

Would that work for you?

Greetings,
Mario

Ok, thank you for clarification. I think I can work around.

Greetings
Matthias