iOS, MOBILE

Xcode 9.3 beta. New tools for detecting battery drain

Last 24th January was a busy day for Apple. Coinciding with the release of iOS 11.3, tvOS 11.3, watchOS 4.3 and macOS 10.13.4 Apple also release a new version of Xcode, Xcode 9.3 beta 1, with multiple features and improvements despite being a minor version.

Xcode 9.3 beta

Energy Organizer

This is one of the most important new features of this new version. Xcode 9.3 beta includes a new version of the Energy Organizer tool that allows developers to detect battery drains on their apps. It seems that after the scandal of battery performance drops for terminals with degraded batteriesApple have decided to be more transparent with the use that terminals do of their batteries, giving users the control to decide if he wants that his terminal lowers its performance or not on high energy consumption peaks.

However, this problem only hides a more important problem under the hood. Sometimes developers do not give enough attention to energy consumption peaks on our apps. To help solve this problem Apple has focused its efforts to improve this tool, hoping it will be helpful to solve this recurrent problem.

This new version will warn you when an app exceeds the medium reasonable energy consumption expected for an app informing if it is caused by CPU, network, Location Services or GPU. Also, it tags if those energy consumption peaks come from foreground or background execution modes, or even when the app was suspended, so these problems will be easier to debug and solve.

Energy Organizer will also be able to warn us about possible battery problems even when the app has been released to the App Store. This will be possible with the new battery crash reports. These reports will be automatically sent to Apple if a user has authorized to share app usage info with them.

64-bit Mode

This is one of the most important changes introduced on macOS 10.13.4 beta 1. When trying to open a 32-bit application, the system warns us that 32-bit apps are not compatible with 64-bit processors and that will not work anymore on future macOS releases. This warning is very like the older one that appeared on iOS 10 when we open an app compiled exclusively on 32-bit architecture. Those applications that were not adapted to compile on 64-bit mode stopped working definitely on iOS 11 and it is assumed that something similar will happen with macOS in the future.

Apple concludes in June 2017 that “macOS High Sierra will be the last macOS release that supports 32- bit apps without compromise”. iOS 11 ended 32-bit applications support last year, promising processors performance improvements.

Apple advises to developers:

“Starting January 31, new apps submitted to Mac App Store must support 64-bit, and Mac app updates and existing apps must support 64-bit starting June 2018.

If you distribute your apps outside Mac App Store, we highly recommend distributing 64-bit binaries to make sure users can continue to run your apps on future macOS versions.”

To be prepared Apple has introduced a new 64-bit testing mode on Xcode 9.3 beta, allowing Mac developers to test apps to verify that they will be compatible with 64-bit mode. To execute this 64-bit testing mode on macOS 10.13.4 we have to follow these steps:

  1. Open terminal
  2. Type command sudo nvram boot-args=”-no32exec”
  3. Reboot

This new testing mode prevents all 32-bit processes to be executed. This way, when launching a 32 bits app, a warning will be displayed informing that app cannot be opened. We must bear in mind that that other software could fail silently, like the 32-bit version of Dashboard and WebKit plugins, preference panel and background processes. 64-bit Mode To disable 64-bit only execution mode we have to proceed:

  1. Open terminal
  2. Execute command sudo nvram boot-args=”
  3. Reboot

Besides all this, 32-bit is no longer available as a compilation option on UI and the compilation system will warn you when you compile your app for macOS on a 32-bit architecture.

Compilation System

Xcode 9.3 features a new option to optimize by size ( -Osize ) We can access new Optimization Level setting within Swift compiler Build Settings. When this mode is selected the Swift compiler optimizes the app to reduce the size of the compilated code. Compiling by module or file option have been moved from a setting on Optimization Level to Compilation Mode, that is a new swift compiler setting on Build Settings panel of Project Editor. Previously that option was combined with other options on Optimization Level setting. Compiling by file compiles only if files have changed, allowing faster compilations. Compiling by module allows a better app global optimization.

Xcode has been also being redesigned to execute more Swift compilation tasks in parallel together with other commands, improving compilation times for Swift projects. It could, however, results on an increment of memory usage on compiling stages.

Additional Features

Xcode 9.3 beta also includes other interesting features like:

  • Debugging. New debug options have been added for Core Animation for iOS and tvOS that previously were only available through Core Animation Instruments. You can access these options on Debug > View Debugging > Rendering.
  • Code Editor. Now editor anti-aliasing can be disabled. Open a terminal and execute defaults write com.apple.dt.Xcode SourceEditorDisableAntialiasing -bool YES If you want to re-enable use the same command with NO as a parameter.
  • Version Control. Versions Comparison View have been redesigned with improved design and scrolling.
  • Project Model. Xcode project file has a new format, being the new format not retro-compatible with old Xcode versions. You can migrate from the older format to the new one selecting the project on the inspector and clicking “Project Format” from the context menu.
  • Server. Reports Navigator has been updated for Xcode Server integrations. Now, repository commits, logs Compilation System Additional Features and test sub-nodes are displayed with code coverage results.

Availability

Xcode beta can be downloaded on Apple Developer Portal for all users enrolled in the developer program. Xcode 9.3 requires a Mac executing macOS 10.13.2 or later, which means that Xcode is no longer compatible with Macs that execute older operating systems like Sierra.

Swift 4.1

Xcode 9.3 will be the very first version to release Swift 4.1 to the public with the new iOS 11.3 API. This new version of Swift includes very improvements. Let’s analyze the three most important improvements.

Equatable protocol allows comparing one instance of a type with another instance. When we have an instruction like 2 == 2 Swift knows how to process it because Int type implements the Equatable protocol and within this protocol, there is a defined method == that specify how the equality condition should be evaluated.

Equatable were implemented until now by default on several basic types like Int, Double, String, among many others. However, when we want to compare a struct or an enum we must conform the Equatable protocol especifing the implementation of its methods.

For example:

struct Movie: Equatable {
 var title: String
 var year: Int
 var genre: String
 var description: String
 static func ==(lhs: Movie, rhs: Movie) -> Bool {
 return lhs.title == rhs.title && lhs.year == rhs.year && lhs.genre == rhs.
genre && lhs.description == rhs.description
 }
}

Fortunately, from Swift 4.1 all types conforming the Equatable protocol generates a == method automatically that compares all object properties with all the properties for the other object. With this feature on Swift 4.1 we have to implement only this:

struct Movie: Equatable {
 var title: String
 var year: Int
 var genre: String
 var description: String
}

Swift 4.1 also provides a default implementation for Hashable protocol. Before Swift 4.1 we have to code something like this:

var hashValue: Int {
 return title.hashValue ^ year.hashValue &* 16777619
}

to return a unique numeric identifier. This was not always easy to implement. Thanks to Swift 4.1 we no longer have to worry about implementation because a single object hash is generated from its properties.

Conditional Conformances

On Swift 4.0 we could not compare two optional types. For example, this code does not compile:

var left: [String?] = ["One", "Two", "Three"]
var right: [String?] = ["Four", "Five", "Six"]
left == right

this is caused because String and [String] conforms Equatable but [String?] does not. This is solved on Swift 4.1 allowing the code above to compile.Conditional Conformance had extended Codable protocol making things much safer.

Given that code:

struct Movie {
 var id = "1234"
 var title = “Blade Runner”
}
var people = [Movie()]
var encoder = JSONEncoder()
try encoder.encode(people)

This code creates an array with a Movie object inside. After that, it tries to code this object to a JSON. That code compiles without problems on Swift 4.0 but throws a fatal runtime error because Movie does not conform Codable. Swift 4.1 fix that problem: Optional, Array, Dictionary and Set only conforms Codable if its contents also conform Codable, making the above code fails when compiling, avoiding the error before it could occur.

flatMap is now (partially) compactMap

The flatMap() method was very helpful on Swift 4.0 when we want to transform each object from a collection and after that remove any of nullable items. Swift Evolution proposal SE-0187 suggest changing that. On Swift 4.1 this nullable flatMap() variant should be renamed to compactMap() to clarify things. Conditional Conformances flatMap is now (partially) compactMap

For example:

let array = ["1", "2", "Fish"]
let numbers = array.compactMap { Int($0) }

That will create an Int array containing the numbers 1 and 2, because “Fish” will fail conversion to Int, return nil, and be ignored.

Reinforced Access Control

Access control is enforced for subclasses of generic classes. A public class may not subclass an internal class. For source compatibility, these new diagnostics are warnings in Swift 4.1.

Conclusion

Xcode 9.3 beta and Swift 4.1 are minor versions but include a lot of very interesting features and tools to improve the quality of our apps and make our code safer. Today Apple has launched Xcode 9.3 beta 1 & beta 2. Following the beta release policy, Apple will release a few more version before iOS 11.3 will be launched.

Cannot wait to see this final release! Give it a try!

Daniel Coello, iOS Team Lead at Intelygenz

Do you want to know more about us?

Follow us

SOCIAL NETWORK

Share This

Share this post with your friends!