Contentful logo

Contentful Community

How to use Sync API in the right way

#1

Hi
the JAVA SDK is providing the SyncQuery but I can not find out how to use it right.
I would like to have an initial call to fetch all entries and all subsequent calls should only deliver changed /deleted entries.
Is the an example how to dio that with the JAVA SDK?

With my current implementation I get every time all entries.

What I actually do is the following:
SyncQuery syncQuery = contentfulClient.sync(synchronizedSpace);
synchronizedSpace = syncQuery.fetch();
Map<String, CDAEntry> entries = synchronizedSpace.entries();

synchronizedSpace is saved for the current session.

0 Likes

#2

Hello,

that is strange. Are you using preview by any chance? On preview you can only use initial sync returning all items.

On all other cases you should be able to use sync as you described. Do you mind sharing more code of the client creation if you are not on preview?

Greetings,
Mario
– Contentful Java SDK Developer –

0 Likes

#3

Hello Mario,
I do not use explicitly the preview.
He the code how I am creating the client
CDAClient.Builder cdaBuilder = CDAClient.builder().setSpace(spaceKey).setToken(accessToken);
if (proxyHost != null && !NO_PROXY.equalsIgnoreCase(proxyHost) && !proxyHost.isEmpty()) {
logger.debug(“Found config-value for explore.contentful.proxy-host {} and port {},”
+ " setup proxy configuration", proxyHost, proxyPort);
OkHttpClient.Builder httpBuilder = cdaBuilder.defaultCallFactoryBuilder();
httpBuilder.proxy(getProxy());
cdaBuilder.setCallFactory(httpBuilder.build());
logger.debug(“Setup proxy configuration with {}”, httpBuilder);
} else {
logger.debug(“Create Client without proxy configuration.”);
}
return cdaBuilder.build();

and I checked again the API token, I use the Content Delivery API token

1 Like

#4

Hello,

you are actually retriving items from the internal sdk cache: Once the first sync returns, it will populate that cache. This cache will be used for retrieving items for the second request, so you always have all the items you needed. If you want to take a look at the deleted entries, the method .deletedEntries on the SyncSpace contains this information.

Greetings,
Mario

0 Likes

#5

Ok, more questions:

What did you mean with ‘take a look at the deleted entries’, there are only deleted entries not the updated one.

I a assume my java-wm memory consummation will grow if the count of content is growing?

Is there an attribute at an Entry I can determine it is from cache or new at the contentful space?

Greetings
Matthias

1 Like

#6

Hey,

Thanks for coming back on us, I am happy to answer your questions:

You were asking on how to get deleted entries in your first post. You get them by looking at the .deletedEntries() method.

I was also misremembering how the caching for sync works. Actually the cache is reset with the fetched items, so there should not be an significant vm size increase. Instead the updated items are getting merged with the ones already existing in the synchronized space you put in as an argument to .sync().

For update entries, you need to create a new sync space in subsequent calls:

    final CDAClient client = CDAClient
      .builder()
      .setToken("b4c0n73n7fu1")
      .setSpace("cfexampleapi")
      .build();
      
    final SynchronizedSpace space = client.sync().fetch();
    System.out.println(space.items().size());
    System.out.println(space.assets().size());
    System.out.println(space.entries().size());
    System.out.println(space.nextSyncUrl());
    
    final SynchronizedSpace latest = client.sync(space.nextSyncUrl().split("=")[1]).fetch();
    System.out.println(latest.items().size());
    System.out.println(latest.assets().size());
    System.out.println(latest.entries().size());
    System.out.println(latest.nextSyncUrl());

Output:

14
4
10
https://cdn.contentful.com/spaces/cfexampleapi/environments/master/sync?sync_token=token1
0
0
0
https://cdn.contentful.com/spaces/cfexampleapi/environments/master/sync?sync_token=token2

The latest space is taking the token from the .nextSyncUrl. Then the new sync space does only contain the changes from in between (none in my instance).

And for the question if there is a date attribute in the entry, specifying the last edit date, indeed there is one as mentioned in our documentation: sys.updatedAt contains the time the entry was updated last.

I hope I could shine some more lights onto the sdk, I am happy to help you more, if I can.

Greetings,
Mario

0 Likes

#7

Thank you!
It seem to work for now.

Greetings
Matthias

0 Likes