Manual change detection in Angular for performance

There are special use cases where you might need to customize change detection in Angular applications for performance reasons. The majority of the time, default change detection is the correct way to go. I’ve worked with several customers recently who were using very large datasets within Angular 8 components and this caused some interesting UI slowdowns as a side-effect.

Fortunately, and unsurprisingly, Angular does allow you modify change detection, for example ChangeDetectionStrategy.OnPush and ChangeDetectorRef. In both cases that I worked on, my recommendation was to lean towards ChangeDetectorRef in order to provide granular, explicit control over when a component updated to reduce performance impacts on the UI.

In one case, the change detection needed to be slowed down and it was suitable to run it on a timer loop. Here’s the pseudo-code, and there are plenty of similar examples on the internet including in the Angular documentation:

import { Component, ChangeDetectorRef, . . .  } from '@angular/core';

constructor(private changeDetector: ChangeDetectorRef) {
    changeDetector.detach();
    setInterval(() => {
      this. changeDetector.detectChanges();
    }, 5000);
  }

In the other use case, the change detection only needed to happen when an Observable from a service was updated. That implementation used a pattern similar to this pseudo-code:

import { Component, ChangeDetectorRef, OnInit } from '@angular/core';

constructor(private stateMgmtService: StateMgmtService, private changeDetector: ChangeDetectorRef) {}

public messages$: Observable<MySpecialArray[]>
public list: Subscription:
public data: any[] = [];

ngOnInit() {
   this.changeDetector.detach(); 
   this.messages$ = this.stateMgmtService.getSomeData();
   this.list = this.message$.subscribe({
      next: x => {
         // . . . do some calculations against x
         this.data = x;
         // Only detect changes on next
         this.changeDetector.detectChanges();
      }
   })

}

And here’s the component.html:

<!-- https://material.angular.io/cdk/scrolling/overview -->
<cdk-virtual-scroll-viewport [itemSize]="8" class="point-viewport">
  <div *cdkVirtualFor="let location of data”>
    {{location}}
  </div>
</cdk-virtual-scroll-viewport>

Caveat Emptor. There are technical debt issues when you implement manual control on Angular change detection. Instead of a nice, loosely coupled approach to handling UI updates, you step into potentially creating inconsistencies between how different components handle updates and this adds complexity to your application. This can also affect how you write unit tests and can introduce unforeseen bugs. With all that said, sometimes you have to make decisions based on your unique requirements and you have to take the best approach for your circumstances.

Easily back up and restore your iPod to Mac Catalina

iTunes is gone but there are a few simple steps to backing up and restoring an iPod to MacOS Catalina without having to purchase software. You can complete the following steps by using tools that are included for free on your Mac.

In my case I had to back up a really old 1st generation iPod Nano that hadn’t been backed up in years, and restore the files to a 2015, 7th Generation Nano. NOTE: I haven’t tried the steps below with the latest generation iPods (post-7th Gen) or iPhone/iPad.

Also, if you tried to use old command line tricks for running your backups like I did, those won’t work out-of-the-box anymore because the latest MacOS updates include System Integrity Protection (SIP). You can disable SIP, but there’s an easier way! Read on…

Before you get started make sure your Mac is updated to the latest version.

Step 1: Connect iPod to your Mac

Make sure the iPod is on and when you connect it to the Mac by USB cable. Within a few seconds you should see two listings appear in the Finder’s sidebar with the name of your iPod under Locations. If the listings don’t appear the most likely culprit is a bad cable, so you’ll need to try another one. 

One of the listings provides an overview of iPod management options, the other listing will let you browse the iPod’s filesystem. In the management listing under the General section I chose to manually manage my music, movies and TV shows. This is important if you don’t want the iPod to automatically sync as soon as you connect it to the Mac.

Step 2: View hidden directories on the iPod

In Finder using the listing that lets you browse your iPod’s filesystem, if you don’t see the iPod_Control directory then you’ll need to unhide file folders. From your keyboard do command + shift + . (that last item is a period on your keyboard) and now you should be able to see all hidden directories including iPod_Control.

Step 3: Copy files from iPod to Mac

Now open another Finder window and create a new directory such as /Documents/iPod_music/

Copy the iPod_Control/Music/ sub-directory from the original Finder window to the new one. Depending on how many music files you have this could take a while.

Step 4: Sync the Music App

Open Music.app on your Mac. You should see your iPod listed under devices. If not then go back to Step 2.

In the top menu bar go to File > Import and select the directory that has your newly copied music files and then press the “open” button. This should import the files so that they are now managed by the Music app.

Step 5: Restore existing iPod or update new iPod

Once the file import in Step 4 is complete, then you can right click on your device name in the Music app and select sync. When the sync is complete, right click on the device name and choose eject. Once the device has finished ejecting you should now be able to browse and play your files!

Extra Credit

For extra credit, in Finder select the listing that lets you manage the iPod settings run Check for Update. I was surprised there was indeed an update for my Nano.

RIP Scott Carpenter – legend, hero and original geek (OG)

I was deeply saddened on October 10th when living legend and true hero Scott Carpenter passed away. Scott was an inspiration to me as one of the original Mercury Seven. The fact that he was a Colorado University alumni helped as well.

The Mercury Seven were the best-of-the-best chosen from a pool of 500 applicants. They were the original geeks who paved the way to placing human beings on the moon. Take a moment to consider that they truly made world history and inspired generations of young men and women to become pilots, astronauts and engineers. And, they helped inspire millions of others to dream about traveling to other planets, exploring solar systems and more. They did it for real.

There aren’t too many people these days that can set the standard as high as Scott and his fellow astronauts. There are few accomplishments these days that can galvanize the entire planet as the first attempts to leave earth.

Scott first flew into orbit on May 24, 1962. Can you believe that was 51 years ago?

Sure Scott and his fellow astronauts weren’t perfect and they have had their share of controversy. Some are trying to cast doubt on Scott’s accomplishments. We shouldn’t let perceived or even real mistakes overshadow that he passed some of the toughest training of our times, he took the risks, and he placed his life on the line to try push the technological envelope farther than it had been pushed before.  He lived the dream for the billions of us who will never get to see earth from space.

I can only hope that there are more people in our life time that can stand on the shoulders of the heros from Mercury Seven, the subsequent legendary missions of Gemini, Apollo and Space Shuttle missions and inspire future generations the way I’ve personally been inspired.