문서 메뉴

문서 홈애플리케이션 개발Atlas Device SDK

변경사항에 대응하기 - React Native SDK

이 페이지의 내용

  • Realm 변경 리스너 등록
  • 컬렉션 변경 리스너 등록
  • 객체 변경 리스너 등록
  • 모든 변경 리스너 제거
  • 알림 한도 변경

Realm 객체와 collection은 항상 데이터를 읽을 때 최신 상태를 반영합니다. Realm은 데이터의 상태가 변경될 때마다 변경 알림을 내보냅니다. 이를 통해 커밋된 쓰기 트랜잭션에 대해 앱을 업데이트할 수 있습니다.

세 가지 유형의 알림 수신기를 등록할 수 있습니다.

  • Realm 리스너는 Realm의 객체가 바뀔 때마다 실행됩니다.

  • 컬렉션 리스너는 특정 쿼리가 새 객체 세트와 일치하거나 일치하는 객체가 변경될 때마다 실행됩니다.

  • 객체 리스너는 특정 객체가 삭제되거나 하나 이상의 속성이 수정될 때마다 실행됩니다.

전체 Realm에 대한 변경 리스너를 등록하려면 Realm의 addListener() 메서드에 콜백 함수를 전달합니다. Realm은 작업이 Realm에서 객체를 추가, 변경 또는 제거할 때마다 리스너를 비동기적으로 호출합니다.

Realm 리스너를 제거하려면 Realm의 removeListener() 메서드에 콜백을 전달합니다.

변경 세부 정보를 위해 객체 및 컬렉션 리스너 사용

Realm은 Realm 리스너 콜백 함수의 변경된 사항에 대한 정보를 전달하지 않습니다. 객체나 컬렉션에서 변경된 사항에 대한 자세한 정보가 필요한 경우 객체 리스너컬렉션 리스너를 사용하세요.

리스너 내부의 예외 처리

변경 리스너에서 addListener() 발생한 예외를 처리하려면 호출을 try...catch 성명서.

// Define a listener callback function
function onRealmChange() {
console.log("Something changed!");
}
// Add the listener callback to the realm
try {
realm.addListener("change", onRealmChange);
} catch (error) {
console.error(
`An exception was thrown within the change listener: ${error}`
);
}
// Remember to remove the listener when you're done!
realm.removeListener("change", onRealmChange);

Realm 객체 컬렉션에 대한 변경 리스너를 등록하려면 컬렉션의 addListener() 메서드에 콜백 함수를 전달합니다. Realm은 리스너가 등록될 때뿐만 아니라 작업이 컬렉션에서 객체를 추가, 변경 또는 제거할 때마다 리스너를 비동기적으로 호출합니다.

컬렉션 리스너를 제거하려면 컬렉션의 removeListener() 메서드에 콜백을 전달합니다.

중요

알림 순서의 중요성

컬렉션 알림 핸들러에서는 항상 삭제, 삽입, 수정의 순서로 변경 사항을 적용합니다. 삭제하기 전에 삽입을 처리하면 예기치 않은 동작이 발생할 수 있습니다.

// You can define a listener for any collection of Realm objects
const dogs = realm.objects("Dog");
// Define a listener callback function for changes to any Dog
function onDogsChange(dogs, changes) {
// Handle deleted Dog objects
changes.deletions.forEach((index) => {
// You cannot directly access deleted objects,
// but you can update a UI list, etc. based on the index.
console.log(`Looks like Dog #${index} has left the realm.`);
});
// Handle newly added Dog objects
changes.insertions.forEach((index) => {
const insertedDog = dogs[index];
console.log(`Welcome our new friend, ${insertedDog.name}!`);
});
// Handle Dog objects that were modified
changes.modifications.forEach((index) => {
const modifiedDog = dogs[index];
console.log(`Hey ${modifiedDog.name}, you look different!`);
});
}
// Add the listener callback to the collection of dogs
try {
dogs.addListener(onDogsChange);
} catch (error) {
console.error(
`An exception was thrown within the change listener: ${error}`
);
}
// Remember to remove the listener when you're done!
dogs.removeListener(onDogsChange);

특정 Realm 객체에 변경 리스너를 등록하려면 객체의 addListener() 메서드에 콜백 함수를 전달합니다. Realm은 객체의 속성이 변경되거나 누군가 객체를 삭제하면 리스너를 호출합니다.

객체 리스너를 제거하려면 객체의 removeListener() 메서드에 콜백을 전달합니다.

// Define a listener callback function for changes to a specific Dog
function onDogChange(dog, changes) {
if (changes.deleted) {
console.log(`dog is deleted: ${changes.deleted}`);
} else {
changes.changedProperties.forEach((prop) => {
console.log(`* the value of "${prop}" changed to ${dog[prop]}`);
});
}
}
// You can define a listener for any Realm object
try {
dog.addListener(onDogChange);
} catch (error) {
console.error(
`An exception was thrown within the change listener: ${error}`
);
}
// Remember to remove the listeners when you're done!
dog.removeListener(onDogChange);

특정 Realm, 객체 또는 컬렉션 인스턴스에서 모든 리스너를 제거하려면 인스턴스의 removeAllListeners() 함수를 호출합니다.

// Remove all listeners from a realm
realm.removeAllListeners();
// Remove all listeners from a collection
dogs.removeAllListeners();
// Remove all listeners from an object
dog.removeAllListeners();

중첩된 문서의 변경 내용이 4단계 아래로 내려가면 변경 알림이 트리거되지 않습니다.

5단계 아래 또는 그 이상의 변경 사항을 수신해야 하는 데이터 구조가 있는 경우 해결 방법은 다음과 같습니다.

  • 스키마를 리팩터링하여 중첩을 줄입니다.

  • 사용자가 수동으로 데이터를 새로 고칠 수 있도록 '푸시하여 새로 고침'과 같은 내용을 추가합니다.

← 데이터 쿼리 - React Native SDK