René's URL Explorer Experiment


Title: Reusable and recyclable NativeScript views · Issue #7469 · NativeScript/NativeScript · GitHub

Open Graph Title: Reusable and recyclable NativeScript views · Issue #7469 · NativeScript/NativeScript

X Title: Reusable and recyclable NativeScript views · Issue #7469 · NativeScript/NativeScript

Description: Is your feature request related to a problem? Please describe. Removing a NativeScript view from the visualtree results in all native views being disposed on Android. On iOS, some native views are not disposed, but their delegates are. T...

Open Graph Description: Is your feature request related to a problem? Please describe. Removing a NativeScript view from the visualtree results in all native views being disposed on Android. On iOS, some native views are ...

X Description: Is your feature request related to a problem? Please describe. Removing a NativeScript view from the visualtree results in all native views being disposed on Android. On iOS, some native views are ...

Opengraph URL: https://github.com/NativeScript/NativeScript/issues/7469

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Reusable and recyclable NativeScript views","articleBody":"**Is your feature request related to a problem? Please describe.**\r\nRemoving a NativeScript view from the visualtree results in all native views being disposed on Android. On iOS, some native views are not disposed, but their delegates are. This behavior seems to come from https://github.com/NativeScript/NativeScript/pull/3912 but I haven't found an explanation for why it is that way.\r\n\r\nSince the view is destroyed, there is no way to do, for example:\r\n\r\n```typescript\r\nconst parent1 = stack1;\r\nconst parent2 = stack2;\r\nconst st  = new StackLayout();\r\nconst bt = new Button()\r\nst.addChild(bt);\r\nparent1.addChild(st);\r\n// st.android != null\r\nparent1.removeChild(st);\r\n// st.android == null\r\nparent2.addChild(st);\r\n// st.android != null\r\n```\r\n\r\nWithout the view being destroyed and created again.\r\n\r\nThis also makes hard to use some plugins like `nativescript-popup`. We've tried creating an angular template and passing the NS views to the plugin, but once we detached the view from it's parent, the native views became null and the popup was empty. Our workaround was to manually use the native views that were created and set `view.parent = null` otherwise the app would crash when closed.\r\n\r\n**Describe the solution you'd like**\r\nNativeScript views could be reusable:\r\n\r\n```typescript\r\nconst parent1 = stack1;\r\nconst parent2 = stack2;\r\nconst st  = new StackLayout();\r\nst.reusable = true;\r\nconst bt = new Button()\r\nst.addChild(bt);\r\nparent1.addChild(st);\r\nconst native = st.android;\r\nparent1.removeChild(st);\r\nnative === st.android // true\r\nparent2.addChild(st);\r\nnative === st.android // true\r\n```\r\n\r\nThis is different from https://github.com/NativeScript/NativeScript/issues/4189, since we're not just reusing the native views, but the whole NS view, so no properties have to be reset. Maybe an additional method (like `view.inflate()`) could be provided to inflate views without the need to attach to the \"dom\".\r\n\r\n**Additional context**\r\nI've only used core and angular flavors, so I'll contextualize it using them:\r\n\r\nAngular/Vue/React/etc flavors of nativescript would create all their views as `reusable`. The angular renderer has [destroyNode](https://angular.io/api/core/Renderer2#destroyNode) method that is unimplemented by `nativescript-angular` because the core already destroys the view when it's detached. [This method is called when the view is not being used anymore](https://github.com/angular/angular/blob/7ca611cd1266ede26a26b17cb31748988ff8d98c/packages/core/src/render3/node_manipulation.ts#L90-L97), so the view can be safely detached and reused. I'm sure the other frameworks have similar approaches.\r\n\r\nThis necessity came up when I was starting to develop a recyclerview plugin with shared pools (which is possible natively, no need for this PR) and general use of `nativescript-popup` and I stumbled upon my views being destroyed whenever I tried to detach them.\r\n\r\nHere's a playground of the current behavior: https://play.nativescript.org/?template=play-tsc\u0026id=eorFFE\u0026v=6\r\n\r\nHere's what happens when we try detaching and attaching: https://play.nativescript.org/?template=play-ng\u0026id=mXwdnl\r\n\r\nIdeally, \"create native view\" should only output once for each label created by \"Create Views\" and none for \"Move Views\".\r\n\r\nPartially inspired by: https://hackernoon.com/react-native-listview-performance-revisited-recycling-without-the-bridge-c4f62d18c7dd?gi=87e16d79933e\r\n\r\n### Use cases\r\n\r\n#### Custom reusable heavy views/view tree in the core\r\n\r\nAllows you to reuse a view or view tree anywhere. Examples: footer that is used in almost every screen only needs to instanced once. Same view pool for multiple ListViews. A conditional view that may be added or not to a ListView item (the current best practice is instantiate all views and set the `visibility` property, or using multiple templates, potentially increasing memory usage).\r\n\r\n```typescript\r\n// viewrecyler.ts\r\nexport class ViewRecycler {\r\n  viewPool = [];\r\n  availableViews = [];\r\n  prefetch = 5;\r\n\r\n  constructor() {\r\n    for (let i = 0; i \u003c this.prefetch; i++) {\r\n      this.generateReusableView();\r\n    }\r\n  }\r\n\r\n  generateReusableView() {\r\n    const view = new HeavyView(); // HeavyView could be a whole view tree\r\n    view.reusable = true;\r\n    view.inflate(); // force creation, we're prefetching.\r\n    this.viewPool.push(view);\r\n    this.availableViews.push(view);\r\n    return view;\r\n  }\r\n\r\n  getView() {\r\n    if (this.availableViews.length === 0) {\r\n      this.generateReusableView(); // if none available, generate a new one\r\n    }\r\n    return this.availableViews.pop();\r\n  }\r\n\r\n  storeView(detachedView: HeavyView) {\r\n    this.availableViews.push(detachedView);\r\n  }\r\n}\r\n\r\nexport const viewRecycler = new ViewRecycler();\r\n\r\n// page.ts\r\n\r\nlet recyclableView;\r\nfunction navigatedTo() {\r\n  const container = getViewById(page, \"viewcontainer\");\r\n  recyclableView = viewRecycler.getView();\r\n  container.addChild(recyclableView);\r\n}\r\n\r\nfunction navigatingFrom() {\r\n  if (recyclableView) {\r\n    recyclableView.parent.removeChild(recyclableView);\r\n    viewRecycler.storeView(recyclableView);\r\n    recyclableView = null;\r\n  }\r\n}\r\n```\r\n\r\nIt's up to the developer to set/reset view properties. In the example above, all properties must be set on `recyclableView = viewRecycler.getView();`, or reset on `viewRecycler.storeView(recyclableView);`. When too many properties are being reset each time (defeating the purpose of recycling), it's recommended to add a new view pool.\r\n\r\n\r\n#### ngFor/Repeater\r\n\r\nWhat could be accomplished with this:\r\n```\r\n\u003cButton *ngFor=\"let item of item;\"\u003e\u003c/Button\u003e\r\n```\r\nThis is already possible, but every time you swap something, angular uses `detach` and `insert`, meaning you get lag. Workaround: use `trackBy` using `index`.\r\n```\r\n\u003cButton *ngReusableFor=\"let item of items; uniqueKey:'myForKey'\"\u003e\u003c/Button\u003e\r\n\u003cButton *ngReusableFor=\"let item of items2; uniqueKey:'myForKey'\"\u003e\u003c/Button\u003e\r\n```\r\n\r\n`ngReusableFor` would have a service that stores views in `myForKey`. No matter how many items you add/remove from `items` and `item2`, the same views would be reused in both of them. **caution: the same template has to be used for both fors**.\r\n\r\n#### Global templates\r\nMy favorite use case.\r\n\r\n```\r\n\u003cGlobalTemplates\u003e\r\n  \u003cng-template templateKey=\"reusablecard\" let-item=\"item\"\u003e\r\n    \u003cCardView\u003e\r\n      \u003cStackLayout\u003e\r\n          \u003cLabel text=\"item.title\"\u003e\u003c/Label\u003e\r\n          \u003cLabel text=\"item.content\"\u003e\u003c/Label\u003e\r\n      \u003c/StackLayout\u003e\r\n    \u003c/CardView\u003e\r\n  \u003c/ng-template\u003e\r\n\u003c/GlobalTemplates\u003e\r\n\r\n...\r\n\u003c!-- use inside a listview, ngfor, or any html --\u003e\r\n\u003cReusableTemplate key=\"reusablecard\" context=\"item\"\u003e\u003c/ReusableTemplate\u003e\r\n```\r\n\r\nEvery time you'd use that template, it'd be a recycled instance from somewhere in the app. You could have horizontal listviews inside a vertical listview and they all share the same pool of recycled components. You could also configure some prefetching (create 5 cards when the app starts and just use them over and over).\r\n\r\n\r\n\r\n\u003cbountysource-plugin\u003e\r\n\r\n---\r\nWant to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/76522545-reusable-and-recyclable-nativescript-views?utm_campaign=plugin\u0026utm_content=tracker%2F12908224\u0026utm_medium=issues\u0026utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin\u0026utm_content=tracker%2F12908224\u0026utm_medium=issues\u0026utm_source=github).\r\n\u003c/bountysource-plugin\u003e","author":{"url":"https://github.com/edusperoni","@type":"Person","name":"edusperoni"},"datePublished":"2019-07-05T14:29:33.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/7469/NativeScript/issues/7469"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:22003466-bf15-77e6-551d-8501296eb4d8
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idAAA0:D6F18:3D51542:54EE5C3:6A60F2BF
html-safe-nonced963394b6305eccc227513170573db541302ab0debbda1cde8b911997c335151
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQUEwOkQ2RjE4OjNENTE1NDI6NTRFRTVDMzo2QTYwRjJCRiIsInZpc2l0b3JfaWQiOiI3NDczNDYyMjczMzAwODIwNjcxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac66306a9286b56257b55359ce5e815f4d2b77b7baeda3fde908f6ca499b6ea130
hovercard-subject-tagissue:464666183
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/NativeScript/NativeScript/7469/issue_layout
twitter:imagehttps://opengraph.githubassets.com/7caa0e1567410c86fe70834fd326333308ebfbcd6a75165f0e03244d0f8b4b92/NativeScript/NativeScript/issues/7469
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/7caa0e1567410c86fe70834fd326333308ebfbcd6a75165f0e03244d0f8b4b92/NativeScript/NativeScript/issues/7469
og:image:altIs your feature request related to a problem? Please describe. Removing a NativeScript view from the visualtree results in all native views being disposed on Android. On iOS, some native views are ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameedusperoni
hostnamegithub.com
expected-hostnamegithub.com
None01a0f3379195d313175de239776b09dd4a079d5b2ea29dd9c37e85cd4cd5e990
turbo-cache-controlno-preview
go-importgithub.com/NativeScript/NativeScript git https://github.com/NativeScript/NativeScript.git
octolytics-dimension-user_id7392261
octolytics-dimension-user_loginNativeScript
octolytics-dimension-repository_id31492490
octolytics-dimension-repository_nwoNativeScript/NativeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id31492490
octolytics-dimension-repository_network_root_nwoNativeScript/NativeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release00ca1a9089c8f2453e5d118d0554c8a26883d159
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/NativeScript/NativeScript/issues/7469#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FNativeScript%2FNativeScript%2Fissues%2F7469
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FNativeScript%2FNativeScript%2Fissues%2F7469
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=NativeScript%2FNativeScript
Reloadhttps://github.com/NativeScript/NativeScript/issues/7469
Reloadhttps://github.com/NativeScript/NativeScript/issues/7469
Reloadhttps://github.com/NativeScript/NativeScript/issues/7469
Please reload this pagehttps://github.com/NativeScript/NativeScript/issues/7469
NativeScript https://github.com/NativeScript
NativeScripthttps://github.com/NativeScript/NativeScript
Please reload this pagehttps://github.com/NativeScript/NativeScript/issues/7469
Notifications https://github.com/login?return_to=%2FNativeScript%2FNativeScript
Fork 1.7k https://github.com/login?return_to=%2FNativeScript%2FNativeScript
Star 25.6k https://github.com/login?return_to=%2FNativeScript%2FNativeScript
Code https://github.com/NativeScript/NativeScript
Issues 771 https://github.com/NativeScript/NativeScript/issues
Pull requests 67 https://github.com/NativeScript/NativeScript/pulls
Discussions https://github.com/NativeScript/NativeScript/discussions
Actions https://github.com/NativeScript/NativeScript/actions
Projects https://github.com/NativeScript/NativeScript/projects
Wiki https://github.com/NativeScript/NativeScript/wiki
Security and quality 0 https://github.com/NativeScript/NativeScript/security
Insights https://github.com/NativeScript/NativeScript/pulse
Code https://github.com/NativeScript/NativeScript
Issues https://github.com/NativeScript/NativeScript/issues
Pull requests https://github.com/NativeScript/NativeScript/pulls
Discussions https://github.com/NativeScript/NativeScript/discussions
Actions https://github.com/NativeScript/NativeScript/actions
Projects https://github.com/NativeScript/NativeScript/projects
Wiki https://github.com/NativeScript/NativeScript/wiki
Security and quality https://github.com/NativeScript/NativeScript/security
Insights https://github.com/NativeScript/NativeScript/pulse
Reusable and recyclable NativeScript viewshttps://github.com/NativeScript/NativeScript/issues/7469#top
https://github.com/edusperoni
edusperonihttps://github.com/edusperoni
on Jul 5, 2019https://github.com/NativeScript/NativeScript/issues/7469#issue-464666183
#3912https://github.com/NativeScript/NativeScript/pull/3912
#4189https://github.com/NativeScript/NativeScript/issues/4189
destroyNodehttps://angular.io/api/core/Renderer2#destroyNode
This method is called when the view is not being used anymorehttps://github.com/angular/angular/blob/7ca611cd1266ede26a26b17cb31748988ff8d98c/packages/core/src/render3/node_manipulation.ts#L90-L97
https://play.nativescript.org/?template=play-tsc&id=eorFFE&v=6https://play.nativescript.org/?template=play-tsc&id=eorFFE&v=6
https://play.nativescript.org/?template=play-ng&id=mXwdnlhttps://play.nativescript.org/?template=play-ng&id=mXwdnl
https://hackernoon.com/react-native-listview-performance-revisited-recycling-without-the-bridge-c4f62d18c7dd?gi=87e16d79933ehttps://hackernoon.com/react-native-listview-performance-revisited-recycling-without-the-bridge-c4f62d18c7dd?gi=87e16d79933e
Post a bounty on it!https://www.bountysource.com/issues/76522545-reusable-and-recyclable-nativescript-views?utm_campaign=plugin&utm_content=tracker%2F12908224&utm_medium=issues&utm_source=github
Bountysourcehttps://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F12908224&utm_medium=issues&utm_source=github
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.