I have an error when i use metric kit

Hello, i need to use metric kit to get system data but when I run the application on my iPhone, the application crash on xcode and I have on the output console this message: (lldb) and i have on my code:

public func cellularDataUsage() -> Any {
        let MXNetworkInit = MXNetworkTransferMetric.init()
        let cellularDownload = MXNetworkInit.cumulativeCellularDownload  
        
        return cellularDownload
    }


this error: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1c0eb7fa8) for the line number 04.


People know why ?


Thank you

Replies

First, have you checked you have not set a breakpoint there ?


If no breakpoint, could you check the value of MXNetworkInit

print(MXNetworkInit)


if it seems OK,

try to change

return cellularDownload as Any


or change the return type to

public func cellularDataUsage() ->

Measurement<UnitInformationStorage>
{

with the expexted UnitInformationStorage

First, thank you for your answer 🙂
Second I don't have set a breackpoint.


When I print MXNetworkInit there is nothing.


I have try you solution and here is the output

https://imgur.com/a/6mttQux


I have change too the return type and nothing :/

From the doc, `MXNetworkTransferMetric` is not a thing you instantiate by yourself.


Maybe you need something like this:

class AppDelegate: UIResponder, UIApplicationDelegate, MXMetricManagerSubscriber {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let shared = MXMetricManager.shared
        shared.add(self)
        
        return true
    }
    
    // Receive daily metrics
    func didReceive(_ payloads: [MXMetricPayload]) {
        // Process metrics
        for payload in payloads {
            if let networkTransferMetrics = payload.networkTransferMetrics {
                let cellularDownload = networkTransferMetrics.cumulativeCellularDownload
                //...
            }
        }
    }
    
    //...
}

And the doc has this sentence:

"A registered app receives reports containing data about the previous 24 hours at most once per day."


You cannot get the metrics at the time you want, it is delivered to the app at most once per day.