user172902 user172902 - 2 months ago 16
Objective-C Question

Xcode console debugging output

I am downloading data from firebase and displaying the data onto a tableView. However, I am finding that every now and then there appears to be duplicated content in my tableView. I initially thought that I have accidentally inserted the same content in my PostService.ps.posts array. However, after I put a break point just before I reload the tableView, I typed the following command in the console and the output is weird.


print PostService.ps.posts


As shown in the output below, item [4] and item [6] has {...} which I dont know what they stand for (This is the first time I am using console command to debug). And in my tableView. I see the the data displayed in the following sequence [1],[2],[3],[3],[5],[5],[6],[7],[8],[9] (for row 0 to row 8). So it appears that row 3 (starting form 0) is getting the same data as row 2 and row 5 is the getting the same data as row 4. Weird?

I am not sure why this is happening and I have no where to start because it doesnt happen every time. I am hoping this console output would help pointing me to the correct way

[1] = 0x000000012f4849b0 {
_postKey = "-KQTh_WDO2IS1rYfLgnU"
_userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
}

[2] = 0x0000000130e61f40 {
_postKey = "-KQTHnE4IIeBR3tyDM3r"
_userId = "1isMS98ZmXYrpCmGb4o5voaAqXH2"
_image = "www.image2.com"
}

[3] = 0x0000000130192b40 {
_postKey = "-KQtN4YG51HOF19FrM8s"
_userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
_image = "www.image3.com"
}

[4] = 0x0000000130192b40 {...}

[5] = 0x0000000130269560 {
_postKey = "-KR2On6u7dRy0GAvE1Gn"
_userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
_image = "www.image4.com"
}

[6] = 0x0000000130269560 {...}

[7] = 0x000000013150f4c0 {
_postKey = "-KQThGLVA-MsviGMsXOS"
_userId = "1isMS98ZmXYrpCmGb4o5voaAqXH2"
_image = "www.image5.com"
}

[8] = 0x000000012fa88890 {
_postKey = "-KQt269uHGM99oFKuJRt"
_userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
_image = "www.image6.com"
}

[9] = 0x000000012f9bcad0 {
_postKey = "-KQThdCAm-PlCsCnXcBZ"
_userId = "SpmZmYwD3Td04sQZxOYeALwuMp03"
}


All my table data are configured based on the data inside my PostService.ps.posts which is of type [Post]
Inside my viewForHeaderInSection (I am using this instead of cellForRowAtIndexPath in my app), I have the following code to configure the table

if let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("TableSectionHeader") as? TableSectionHeader {
let post = PostService.ps.posts[section]
var image: UIImage?
if let url = post.imageUrl {
image = DiscoverVC.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, image: image)
cell.delegate = self

return cell

} else {
return TableSectionHeader()
}

Answer

Eventually, I came to know the reason why some elements are showing description as { ... }. It is due to the duplicate reference objects printing. In console original object letting us to see the contents, but the duplicates objects just showing like { ... }. In your case you have the [3] and [4] elements and [5] and [6] elements has the same reference, Hence duplicate elements showing { ... }, so contents also the same thats why your are getting duplicate values. For this I have tried one simple example. Hope it will give some clarity on that.

import UIKit

class ViewController: UIViewController {

    class model {
        let first = "first"
        let second = "second"
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var array = [model]()

        let obj = model()
        let obj1 = model()

        array.append(obj) // original objct
        array.append(obj) // duplicate because classes are referece types
        array.append(obj) // duplicate because classes are referece types
        array.append(obj1) // new Object

        print(array)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

output:

(lldb) print array
([SampleDebugApp.ViewController.model]) $R0 = 4 values {
  [0] = 0x00007fed8ac93990 (first = "first", second = "second")
  [1] = 0x00007fed8ac93990 {...}
  [2] = 0x00007fed8ac93990 {...}
  [3] = 0x00007fed8ac939d0 (first = "first", second = "second")
}
(lldb)

NOTE: It happens only on the array of class object. because class are the reference type.