Atlas Device SDK는 더 이상 사용되지 않습니다. 자세한 내용은 지원 중단 페이지 를 참조하세요.
버전 realm@10.5.0의 새로운 기능
UUID (범용 16 고유 식별자)는UUID 바이트 고유 값Realm.BSON.UUID 입니다. 은(는) BSON ()의 일부로 Realm 패키지 와 함께 번들로 제공됩니다.
객체의 고유 식별자로 UUID를 사용할 수 있습니다. UUID는 인덱싱이 가능하며 프라이머리 키로 사용할 수 있습니다.
class Profile extends Realm.Object { static schema = { name: 'Profile', primaryKey: '_id', properties: { _id: 'uuid', name: 'string', }, }; }
class Profile extends Realm.Object<Profile> { _id!: Realm.BSON.UUID; name!: string; static schema: ObjectSchema = { name: 'Profile', primaryKey: '_id', properties: { _id: 'uuid', name: 'string', }, }; }
사용법
속성을 UUID으로 정의하려면 객체 모델에서 해당 유형을 "uuid" 로 설정합니다. 쓰기 트랜잭션(write transaction) 내에서 Realm 객체를 만듭니다. 객체의 고유 식별자 속성을 임의의 값으로 설정하려면 new UUID()를 호출합니다. 또는 문자열을 new
UUID() 에 전달하여 고유 식별자 속성을 특정 값으로 설정합니다.
예시
다음 CreateProfileInput 예제에서는 _id 필드에 대해 uuid 유형의 Profile Realm.Object 를 만듭니다.
CreateProfileInput 컴포넌트는 다음을 수행합니다.
useRealm()훅을 호출하여 열린 영역 인스턴스에 대한 액세스 권한을 가져옵니다.- 이름 상태 변수를 생성합니다.
- 프로필의 이름을 나타냅니다.
- 쓰기 트랜잭션(write transaction)을 수행하는
createProfile메서드를 만듭니다. 그리고 - 이 쓰기 트랜잭션(write transaction)에서는 " name " 상태 변수의
name값과 새UUID객체의_id값을 사용하여Profile객체를 만듭니다.
- 쓰기 트랜잭션(write transaction)을 수행하는
- 사용자가 프로필 이름을 입력할 수 있는
TextInput구성요소를 - 렌더링합니다. 사용자가 " 프로필 생성" 버튼을 누르면
createProfile메서드가 호출되고Profile객체가 생성됩니다.
- 사용자가 프로필 이름을 입력할 수 있는
1 const CreateProfileInput = () => { 2 const realm = useRealm(); 3 const [name, setName] = useState(''); 4 5 // createProfile creates a new 'Profile' Realm Object with a new UUID based on user input 6 const createProfile = () => { 7 realm.write(() => { 8 realm.create('Profile', { 9 name, 10 _id: new Realm.BSON.UUID(), 11 }); 12 }); 13 }; 14 return ( 15 <View> 16 <TextInput 17 placeholder='Name' 18 onChangeText={setName} 19 /> 20 <Button 21 title='Create Profile' 22 onPress={createProfile} 23 /> 24 </View> 25 );
1 const CreateProfileInput = () => { 2 const realm = useRealm(); 3 const [name, setName] = useState(''); 4 5 // createProfile creates a new 'Profile' Realm Object with a new UUID based on user input 6 const createProfile = () => { 7 realm.write(() => { 8 realm.create('Profile', { 9 name, 10 _id: new Realm.BSON.UUID(), 11 }); 12 }); 13 }; 14 return ( 15 <View> 16 <TextInput 17 placeholder='Name' 18 onChangeText={setName} 19 /> 20 <Button 21 title='Create Profile' 22 onPress={createProfile} 23 /> 24 </View> 25 );