WidgetCacheManager.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. local WidgetCacheManager = class("WidgetCacheManager")
  2. function WidgetCacheManager:ctor()
  3. self._widgetPoolGroup = {}
  4. end
  5. function WidgetCacheManager:GetWiget(widgetConfig)
  6. local widgetPool = self._widgetPoolGroup[widgetConfig]
  7. if widgetPool then
  8. local count = #widgetPool
  9. if count > 0 then
  10. local widget = widgetPool[count]
  11. widgetPool[count] = nil
  12. return widget
  13. end
  14. end
  15. end
  16. function WidgetCacheManager:Create(widgetConfig)
  17. local widget = self:GetWiget(widgetConfig)
  18. if widget then
  19. widget:autorelease()
  20. return widget
  21. else
  22. local parent = cc.Node:create()
  23. GUI:LoadExport(parent, widgetConfig)
  24. local item = parent:getChildByName("Node")
  25. item:removeFromParent()
  26. widget = item
  27. end
  28. return widget
  29. end
  30. function WidgetCacheManager:Destroy(widgetConfig, widget)
  31. if widget and not tolua.isnull(widget) then
  32. widget:retain()
  33. self._widgetPoolGroup[widgetConfig] = self._widgetPoolGroup[widgetConfig] or {}
  34. local widgetPool = self._widgetPoolGroup[widgetConfig]
  35. widgetPool[#widgetPool + 1] = widget
  36. end
  37. end
  38. function WidgetCacheManager:Clear()
  39. for poolName, widgetPool in pairs(self._widgetPoolGroup) do
  40. for _, widget in pairs(widgetPool) do
  41. if widget:getReferenceCount() > 2 then
  42. print("!!!!!缓存泄露,缓存池:", poolName, "对象:", widget, widget:getReferenceCount())
  43. end
  44. widget:release()
  45. end
  46. end
  47. self._widgetPoolGroup = {}
  48. end
  49. function WidgetCacheManager:WidgetPoolCapacity(poolName, capacity)
  50. local widgets = {}
  51. for i = 1, capacity do
  52. local widget = self:Create(poolName)
  53. if widget then
  54. widgets[#widgets + 1] = widget
  55. end
  56. end
  57. for _, widget in pairs(widgets) do
  58. self:Destroy(poolName, widget)
  59. end
  60. end
  61. function WidgetCacheManager:GetCount()
  62. local count = 0
  63. for _, widgetPool in pairs(self._widgetPoolGroup) do
  64. for _, _ in pairs(widgetPool) do
  65. count = count + 1
  66. end
  67. end
  68. return count
  69. end
  70. function WidgetCacheManager:ShowReferenceCount()
  71. for poolName, widgetPool in pairs(self._widgetPoolGroup) do
  72. local count = 0
  73. for _, widget in pairs(widgetPool) do
  74. count = count + 1
  75. end
  76. print("poolName : " .. poolName, "总缓存数 : " .. count)
  77. end
  78. end
  79. return WidgetCacheManager