Open koreahn opened 10 months ago
Hello, @koreahn and sorry to hear you're experiencing this. Have a couple of questions and requests to see if we can help unblock you.
It looks like you're using v5.X of Amplify, so are you able to add logging to your app via the Console Logger? This will allow us to turn the logging level to debug
and get a little more information about what's happening with the network requests when attempting to make that update. Looking to see if there's an error from the AppSync side that would give more details about why it's having trouble resolving on the backend. Using the Hub events emitted for DataStore may help as well to isolate this.
Outside of this, can you clarify if you are using auto-merge or OCC for conflict resolution? Thanks!
Hello, @koreahn and sorry to hear you're experiencing this. Have a couple of questions and requests to see if we can help unblock you.
It looks like you're using v5.X of Amplify, so are you able to add logging to your app via the Console Logger? This will allow us to turn the logging level to
debug
and get a little more information about what's happening with the network requests when attempting to make that update. Looking to see if there's an error from the AppSync side that would give more details about why it's having trouble resolving on the backend. Using the Hub events emitted for DataStore may help as well to isolate this.Outside of this, can you clarify if you are using auto-merge or OCC for conflict resolution? Thanks!
Hello. Thank you for your reply. I added logging like below.
import {Logger} from 'aws-amplify';
import {Hub} from 'aws-amplify';
...
const logger = new Logger('AuthContext');
...
...
const listener = Hub.listen('datastore', async hubData => {
const {event, data} = hubData.payload;
if (event === 'networkStatus') {
console.log(`User has a network connection: ${data.active}`);
}
});
...
...
const updateDeviceToken = async () => {
console.log('Entering updateDeviceToken');
logger.debug('Entering updateDeviceToken');
if (deviceToken) {
console.log('deviceToken is present');
logger.debug('deviceToken is present');
if (dbUser && deviceToken) {
if (dbUser && dbUser.token !== deviceToken) {
console.log('auth', deviceToken);
logger.debug('auth', deviceToken);
console.log('dbUser.token', dbUser.token);
logger.debug('dbUser.token', dbUser.token);
console.log('dbUser.id', dbUser.id);
logger.debug('dbUser.id', dbUser.id);
try {
const newUser = await DataStore.query(User, user =>
user.id.eq(dbUser.id),
);
console.log('user1', newUser[0]);
logger.debug('user1', newUser[0]);
const dbuser = await DataStore.save(
User.copyOf(newUser[0], updated => {
updated.token = deviceToken;
}),
);
console.log('user2', dbuser);
logger.debug('user2', dbuser);
setDbUser(dbuser);
} catch (err) {
console.err('update token error', err);
logger.error('update token error', err);
}
}
} else {
console.log('dbUser.token is the same as deviceToken');
logger.debug('dbUser.token is the same as deviceToken');
}
} else {
console.log('deviceToken is not present');
logger.debug('deviceToken is not present');
}
};
Belows are the logs. It's same as before. And the log of the catch clause was not printed.
LOG User has a network connection: true
LOG User has a network connection: true
LOG Entering updateDeviceToken
LOG deviceToken is present
LOG auth tokenaaaaa <<<<<<<<<< phisical device token.
LOG dbUser.token tokenbbbbb <<<<<<<<<< saved token in DynamoDB earlier
LOG dbUser.id 73e114d0-6c73-40d0-9b18-c015ae9edd8d
LOG user1 {....."token": "tokenbbbbb",.....}
LOG user2 {....."token": "tokenaaaaa",.....}
I am very new to using Amplify. Did I apply the logs properly? And I don't know what auto-merge and OCC are. Can you tell me how to check them?
Let me know if you need anything else. Thank you.
Hi @koreahn, Auto Merge and OCC (Optimistic Concurrency) are conflict resolution strategies you can use with DataStore enabled.
For more info on how they work and differ in behavior please refer to the AppSync documentation:
I think what @cwomack was referring to is adding this to your app where you are configuring Amplify.
Amplify.Logger.LOG_LEVEL = 'DEBUG'
This will log all of the Amplify events for all categories
Thank you @chrisbonifacio.
@cwomack I am not using auto-merge or OCC. Is this the cause of my problem?
Hi @koreahn, Auto Merge and OCC (Optimistic Concurrency) are conflict resolution strategies you can use with DataStore enabled.
For more info on how they work and differ in behavior please refer to the AppSync documentation:
I think what @cwomack was referring to is adding this to your app where you are configuring Amplify.
Amplify.Logger.LOG_LEVEL = 'DEBUG'
This will log all of the Amplify events for all categories
Can you help me? What should I do?
@koreahn what conflict resolution strategy are you using? Custom Lambda?
Hi @koreahn following up here - have you had a chance to see the comment from @chrisbonifacio above?
Hello @chrisbonifacio, @nadetastic.
I am very new to Amplify, so I don't know what the conflict resolution strategy is. I am solely using the DataStore object, nothing else.
How can I check it?
Thanks for the replies on this, @koreahn. It sounds like you could be using AutoMerge as the default conflict resolution strategy if you haven't made any changes yourself.
Could you share your schema and possibly the code that's running queries of the object for the second device? Also, are you using the SQLite adapter? Any further reproduction steps or clarity on differences for each device (i.e. code they are running, if they are being opened at same time, etc.) would be helpful as well! Thanks.
Thank you very much @cwomack.
I have not made any changes. below is my scheme of User table.
type EagerUser = {
readonly [__modelMeta__]: {
identifier: ManagedIdentifier<User, 'id'>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
readonly sub?: string | null;
readonly phoneNumber: string;
readonly userName?: string | null;
readonly email?: string | null;
readonly role?: string | null;
readonly token?: string | null;
readonly subRole?: SubRole | keyof typeof SubRole | null;
readonly createdId?: string | null;
readonly createdName?: string | null;
readonly updatedId?: string | null;
readonly updatedName?: string | null;
readonly createdAt?: string | null;
readonly updatedAt?: string | null;
}
type LazyUser = {
readonly [__modelMeta__]: {
identifier: ManagedIdentifier<User, 'id'>;
readOnlyFields: 'createdAt' | 'updatedAt';
};
readonly id: string;
readonly sub?: string | null;
readonly phoneNumber: string;
readonly userName?: string | null;
readonly email?: string | null;
readonly role?: string | null;
readonly token?: string | null;
readonly subRole?: SubRole | keyof typeof SubRole | null;
readonly createdId?: string | null;
readonly createdName?: string | null;
readonly updatedId?: string | null;
readonly updatedName?: string | null;
readonly createdAt?: string | null;
readonly updatedAt?: string | null;
}
export declare type User = LazyLoading extends LazyLoadingDisabled ? EagerUser : LazyUser
export declare const User: (new (init: ModelInit<User>) => User) & {
copyOf(source: User, mutator: (draft: MutableModel<User>) => MutableModel<User> | void): User;
}
And below is query of the object.
await DataStore.query(User, user =>
user.sub.eq(authUser?.attributes?.sub),
)
.then(users => {
setDbUser(users[0]);
})
.catch(e => {
console.error(e);
setDbUser(null);
});
I dont use SQLite adapter.
I have conducted tests using multiple versions of iOS and Android simulators, as well as physical Android devices. However, I have encountered inconsistency in updates, experiencing cases where updates occur on the same device while other times they do not. I haven't been able to identify any specific rules governing this behavior.
I am not sure if I am answering correctly or not. If these are not correct answer, could you provide more specific details about what tasks I can handle please?
Before opening, please confirm:
JavaScript Framework
React Native
Amplify APIs
DataStore
Amplify Version
v5
Amplify Categories
Not applicable
Backend
Amplify CLI
Environment information
Describe the bug
I want to save the user token to DB(DynamoDB), so I checked and saved in App.js. During testing, I noticed that the token often doesn't update. So I tested the token alternately to two different simulators.
Belows are my code.
Belows are logs.
LOG Entering updateDeviceToken LOG deviceToken is present LOG auth tokenaaaaa <<<<<<<<<< phisical device token. LOG dbUser.token tokenbbbbb <<<<<<<<<< saved token in DynamoDB earlier LOG dbUser.id 73e114d0-6c73-40d0-9b18-c015ae9edd8d LOG user1 {....."token": "tokenbbbbb",.....} LOG user2 {....."token": "tokenaaaaa",.....}
On the log, the update appears to be normal. But when I check the actual data on the amplify console, it doesn't update and still shows 'tokenbbbbb'.
And sometimes there is also a problem with the 'User' table query. The 'User' table has not updated token to tokenbbbbb, but when I run the program again and query the 'User' table, it is inquired to tokenbbbbbbbb. (Actual DB shows tokenaaaa in Amplify console).
Several tables have been created and are being used, but this problem only occurs in the 'User' table.
Expected behavior
The token of the physical device shall be updated to DynamoDB
Reproduction steps
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response