I am attempting to re-join all layers within a group to another layer ("Main.shp") outside of this group. The idea is for users to change values in one of the group's layers. As these layers are initially joined, the have to be re-joined in order to see the updated values in "Main.shp". So the code below basically:
- Identifies the "Main.shp" layer
- Identifies all layers within a specified group
- Defines the field parameters used for the joining process
- Removes the joins
- Adds the joins back to "Main.shp"
- Refreshes "Main.shp"
When the code finishes, there is a progress bar shown in the Status bar but it hangs. If I go see the Join properties of "Main.shp", I noticed that the Memory cache
is not enabled for some layers (this changes randomly) whereas intially it was enabled for all layers:
And no layers can be viewed, they just become invisible. I have to restart QGIS again in order to see the updated values in "Main.shp".
After the comment by @Matte, I am now fairly certain that the cause of this issue is due to the caching of layers. If one joined layer is not cached, the "Main.shp" will not load and eventually crashes QGIS. I have edited the code below. Unfortunately when running the function, not all of the layers are immediately cached. I have to run it a few times before it works for all layers.
Is there a way to ensure that all joined layers are cached?
Code:
shp = QgsMapLayerRegistry.instance().mapLayersByName( "Main" )[0]
shpField = 'ID'
root = QgsProject.instance().layerTreeRoot()
subGroup = root.findGroup('Group')
def update():
for child in subGroup.children():
if isinstance(child, QgsLayerTreeLayer):
shp.removeJoin(child.layer().id())
childField = 'ID'
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = child.layer().id()
joinObject.joinFieldName = childField
joinObject.targetFieldName = shpField
shp.addJoin(joinObject)
shp.createJoinCaches()
update()
shp.triggerRepaint()
Memory cache
is still not enabled for all joined layers but the map refreshes and can see the results of the updated values almost immediately. Going to toy some more with this and see if it hangs again or not.