|
24 | 24 |
|
25 | 25 | import io.objectbox.annotation.apihint.Beta;
|
26 | 26 | import io.objectbox.annotation.apihint.Internal;
|
| 27 | +import io.objectbox.internal.CursorFactory; |
27 | 28 | import io.objectbox.relation.ToMany;
|
28 | 29 |
|
29 | 30 | @SuppressWarnings({"unchecked", "SameParameterValue", "unused", "WeakerAccess", "UnusedReturnValue"})
|
@@ -121,6 +122,8 @@ protected static native long collect004000(long cursor, long keyIfComplete, int
|
121 | 122 |
|
122 | 123 | native void nativeSetBoxStoreForEntities(long cursor, Object boxStore);
|
123 | 124 |
|
| 125 | + native long nativeGetCursorFor(long cursor, int entityId); |
| 126 | + |
124 | 127 | protected final Transaction tx;
|
125 | 128 | protected final long cursor;
|
126 | 129 | protected final EntityInfo entityInfo;
|
@@ -248,11 +251,16 @@ public boolean isClosed() {
|
248 | 251 | return closed;
|
249 | 252 | }
|
250 | 253 |
|
| 254 | + /** |
| 255 | + * Note: this returns a secondary cursor, which does not survive standalone. |
| 256 | + * Secondary native cursors are destroyed once their hosting Cursor is destroyed. |
| 257 | + * Thus, use it only locally and don't store it long term. |
| 258 | + */ |
251 | 259 | protected <TARGET> Cursor<TARGET> getRelationTargetCursor(Class<TARGET> targetClass) {
|
252 |
| - // minor to do: optimize by using existing native cursor handle? |
253 |
| - // (Note: Cursor should not destroy the native cursor then.) |
254 |
| - |
255 |
| - return tx.createCursor(targetClass); |
| 260 | + EntityInfo entityInfo = boxStoreForEntities.getEntityInfo(targetClass); |
| 261 | + long cursorHandle = nativeGetCursorFor(cursor, entityInfo.getEntityId()); |
| 262 | + CursorFactory<TARGET> factory = entityInfo.getCursorFactory(); |
| 263 | + return factory.createCursor(tx, cursorHandle, boxStoreForEntities); |
256 | 264 | }
|
257 | 265 |
|
258 | 266 | /**
|
|
0 commit comments