Is collection shared?

How can I find out, if a collection is shared (required for generic code)?

With an object, I could test:
<code 4D>
C_BOOLEAN($0) //Is shared
C_OBJECT($1) //Object to test

$0:=($1.__LockerID#Null)

</code 4D>
But this doesn’t work with a collection.

The following code does work, but is a hell of a hack. Any better solutions?

<code 4D>
C_BOOLEAN($0) //Is shared
C_COLLECTION($1) //Collection to test

C_TEXT($OnErrorMethod_t)
C_COLLECTION($Shared_c)

Case of
: (Count parameters=0) //Error Method
: ($1=Null)
Else
$OnErrorMethod_t:=Method called on error
ON ERR CALL(Current method name)
$Shared_c:=New shared collection($1)
ON ERR CALL($OnErrorMethod_t)
$0:=(Value type($Shared_c[0])=Is collection)
End case

</code 4D>

The approach I use is to just copy the collection to a shared collection. It may already be a shared collection, but you can ensure you have one when interacting with Storage or passing data to another process. It could be a waste of cycles, but probably not an issue unless you are dealing with a lot of large shared collections.

I don’t find out this way, if the collection is shared.
But if I want to convert a collection to a shared collection, then I can do this in a proper recursion (for nested objects/collections). Just copy could result in the following problem:
<code 4D>
$Collection_c:=New shared collection(New shared collection(New shared collection)) //->Error

</code 4D>

Right, you need a recursive method to copy a collection (shared or not) to a shared collection. There is an example here:

<
https://kb.4d.com/assetid=78197

What is the use case that you need to know the collection is shared?

Use case: e.g. copy a collection to a target.
Similar to “OB_CopyToSharedCollection”, with the difference, that the method is generic.
“OB_CopyToAnyCollection” (shared or unshared)

Hi,

There is a new feature allowing you to copy a standard object or a standard collection as shared
–> https://blog.4d.com/objects-corner-easy-sharing-other-good-news/

1 Like

:slightly_smiling_face: