まめーじぇんと@Tech

技術ネタに関して (Android, GAE, Angular). Twitter: @mame01122

リストプロパティ内のindex

GAEのリストプロパティにて、TextやBlobなどのIndexされないものと、
IndexされるStringなどのデータを一緒に入れたときの話。

GAEのドキュメントには、下記の英語の引用通りですが、

・Indexされるものが先 (Stringなど。どうやらnullも含むらしい)
・Indexされないものが後 (BlobやTextなど)

とのことです。

If value is a Collection, the values will be stored in the datastore with the collection's iteration order with one caveat: all indexed values will come before all unindexed values (this can occur if the Collection contains both values that are normally indexed like strings, and values that are never indexed like Blob, Text and EmbeddedEntity).

Note that Blob, Text and EmbeddedEntity property values are never indexed by the built-in single property indexes. To store other types without being indexed, use setUnindexedProperty.

僕がハマったのは、このnull。
今作っているサービスでは、List property内のTextを消す(nullにする)
ということをやるんですが、
例えば

Text 1
Text 2
Text 3

という順番でText 2をnullにすると、

Text 1
null
Text 3
になっていてほしいんですが、それが何故か

null
Text 1
Text 3

になってしまっていました。

これは上記の引用の通りなんですが、
setPropertyはindexされるものを先に、indexされないものが後になるようなので、
上記のような挙動になるようです。

これを、

Text 1
null
Text 3

のような順番にするには、
setUnindexedPropertyを使う必要があるようです。
使い方はsetPropertyと同じで、
entity.setUnindexedProperty();
とやればOKとのことです。

こうすれば、
Text 1
null
Text 3
のような順番になるようです。